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

ArrayList(int initialCapacity) won't init the Array size

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8u25
    • core-libs

      FULL PRODUCT VERSION :
      Oracle Corporation 1.7.0_65 - OS : Windows 7, x86

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 x86 - updated with Windows update

      A DESCRIPTION OF THE PROBLEM :
      Using the constructor with initialCapacity, then use set() will throw an Exception although the array is created, but size isn't set correctly.

      Using ensureCapacity() won't work neither because it is based on the elementData array instead of size.

      There are other side effects because of the static DEFAULT_CAPACITY with ensureCapacity().

      The only way to make this work is to use add() as many time as required after using the constructor.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      List test = new ArrayList(10);
      test.set(5, "test");

      note that - test.size() = 0 instead of 10

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      test.size() returns 10
      set(5, ...) works
      ACTUAL -
      throws an Exception IndexOutOfBoundsException

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 0

      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      using add() as many time as initialCapacity after the constructor

            psandoz Paul Sandoz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: