-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
None
-
None
-
JDK-1.8.0_111.
-
generic
-
generic
The JavaDoc for the ArrayList() constructor says
Constructs an empty list with an initial capacity of ten.
but in fact it creates an ArrayList whose elementData is an Object[0]. Adding an element to the ArrayList causes ensureCapacity() to replace elementData with an Object[DEFAULT_CAPACITY]. Compare that to calling ArrayList(10) where I can add an element without allocating a new elementData.
I found this by noticing that calling ArrayList.add(E) caused an allocation and a garbage collection in a place where I was hoping to avoid a garbage collection. Using Array(10) solves my problem. I think either the JavaDoc for ArrayList() is misleading, or the implementation does not match the JavaDoc.
Constructs an empty list with an initial capacity of ten.
but in fact it creates an ArrayList whose elementData is an Object[0]. Adding an element to the ArrayList causes ensureCapacity() to replace elementData with an Object[DEFAULT_CAPACITY]. Compare that to calling ArrayList(10) where I can add an element without allocating a new elementData.
I found this by noticing that calling ArrayList.add(E) caused an allocation and a garbage collection in a place where I was hoping to avoid a garbage collection. Using Array(10) solves my problem. I think either the JavaDoc for ArrayList() is misleading, or the implementation does not match the JavaDoc.
- duplicates
-
JDK-8143020 no-arg ArrayList constructor specification has confusing wording about capacity
-
- Open
-