ArrayList: how does the size increase?

后端 未结 19 761
既然无缘
既然无缘 2020-11-29 18:11

I have a basic question on Java ArrayList.

When ArrayList is declared and initialized using the default constructor, memory space for 10 el

相关标签:
19条回答
  • 2020-11-29 18:41

    when a ArrayList is declared and initialized using default constructor, memory space for 10 elements will be created. now, when i add 11 th element, what happens is

    ArrayList create a new object with the following size

    i.e OldCapacity*3/2+1 = 10*3/2+1 =16

    0 讨论(0)
  • 2020-11-29 18:41

    What happens is a new Array is created with n*2 spaces, then all items in the old array are copied over and the new item is inserted in the first free space. All in all, this results in O(N) add time for the ArrayList.

    If you're using Eclipse, install Jad and Jadclipse to decompile the JARs held in the library. I did this to read the original source code.

    0 讨论(0)
  • 2020-11-29 18:43
    static int getCapacity(ArrayList<?> list) throws Exception {
                Field dataField = ArrayList.class.getDeclaredField("elementData");
                dataField.setAccessible(true);
                return ((Object[]) dataField.get(list)).length;
        }
    

    use the above method to check the size when the arraylist is being modified.

    0 讨论(0)
  • 2020-11-29 18:48

    Size of ArrayList increases with n+n/2+1 always.

    0 讨论(0)
  • 2020-11-29 18:49

    Up to JDK 6 the the capacity grow with formula newCapacity = (oldCapacity * 3/2) + 1.

    In JDK 7 and above the formula changes to newCapacity = oldCapacity + (oldCapacity >> 1).

    So if initial capacity is 10 then new capacity will be 16 in JDK6 and 15 in above JDK7

    0 讨论(0)
  • 2020-11-29 18:50

    Sun's JDK6:

    I believe that it grows to 15 elements. Not coding it out, but looking at the grow() code in the jdk.

    int newCapacity then = 10 + (10 >> 1) = 15.

    /**
     * Increases the capacity to ensure that it can hold at least the
     * number of elements specified by the minimum capacity argument.
     *
     * @param minCapacity the desired minimum capacity
     */
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }
    

    From the Javadoc, it says this is from Java 2 and on, so its a safe bet in the Sun JDK.

    EDIT : for those who didn't get what's the connection between multiplying factor 1.5 and int newCapacity = oldCapacity + (oldCapacity >> 1);

    >> is right shift operator which reduces a number to its half. Thus,
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    => int newCapacity = oldCapacity + 0.5*oldCapacity;
    => int newCapacity = 1.5*oldCapacity ;

    0 讨论(0)
提交回复
热议问题