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

XMLWordPrintable

    • Type: Bug
    • Resolution: Duplicate
    • Priority: P4
    • None
    • Affects Version/s: None
    • Component/s: core-libs
    • None
    • Environment:

      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.

            Assignee:
            Chris Hegarty
            Reporter:
            Peter Kessler
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: