Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8170690

ArrayList() says it constructs a list with a capacity of ten but it does not.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • None
    • core-libs
    • None
    • JDK-1.8.0_111.

      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.

            chegar Chris Hegarty
            pbk Peter Kessler
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: