Don't answer during the interview that the initial capacity of ArrayList is 10! ! ! There is a better answer here!

Friends who have used ArrayList or memorized interview questions know ArrayListthe initial capacity 10.
But this answer is not entirely correct, in jdk1.2to jdk1.6the ArrayListsource, on the constructor does is to create an initial capacity of 10 containers.
The excerpts jdk_1.6of the source code 1.2到1.6directly create an array of length 10,
Insert picture description here
but jdk_1.7the source code in is written like this. When
Insert picture description here
calling the constructor, the following
Insert picture description here
instructions are from the jdk_1.7beginning, when you do the new ArrayList();创建的是一个空数组initial capacity is not 10, but an empty array


From the jdk_1.2beginning to the end jdk_1.6, an array of ArrayListlength is created directly 10.

Are there any questions about the jdk1.7initial capacity 0and how did it expand?
In jdk_1.7the ArrayListdefinition of a constant value is 10
Insert picture description here
the time constant during the expansion, will be compared and the minimum capacity of the current container, taken as the maximum capacity of the new vessel
such as when you add the first call to add elements, Will trigger expansion

public boolean add(E e) {
    
    
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}

Enter ensureCapacityInternal(size + 1);, it was an empty container at the beginning, so the size=0incomingminCapacity=1

private void ensureCapacityInternal(int minCapacity) {
    
    
     if (elementData == EMPTY_ELEMENTDATA) {
    
    
         minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
     }
     ensureExplicitCapacity(minCapacity);
 }

You will find that minCapacityto be re-assigned 10 ( DEFAULT_CAPACITY=10)
passed ensureExplicitCapacity(minCapacity);then minCapacity=10
the following is the method body:

private void ensureExplicitCapacity(int minCapacity) {
    
    
    modCount++;
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}

modCount++Is in the parent class AbstarctList, and
then called the expansiongrow(10)

private void grow(int minCapacity) {
    
    
    int oldCapacity = elementData.length;// oldCapacity = 0
    int newCapacity = oldCapacity + (oldCapacity >> 1);// newCapacity = 0
    if (newCapacity - minCapacity < 0) // true
        newCapacity = minCapacity;// newCapacity = 10
    if (newCapacity - MAX_ARRAY_SIZE > 0) // false , MAX_ARRAY_SIZE =Integer.MAX_VALUE - 8;
        newCapacity = hugeCapacity(minCapacity);
    elementData = Arrays.copyOf(elementData, newCapacity);//创建了长度为10的数组
}

This is the expansion mechanism code in jdk1.7,
which int newCapacity = oldCapacity + (oldCapacity >> 1);is often asked during the interview, 1.5倍the length of the original array


Furthermore, ArrayList is allowed to set the initial capacity by itself, just call the construction method with parameters, and then use this 1.5倍initial capacity to expand the capacity according to the initial capacity currently set.

For articles about ArrayList, please see my other blogs:

Get ArrayList, LinkedList, HashMap, HashSet in one article ----- ArrayList of source code interpretation

Do you know in which cases ArrayList will report java.util.ConcurrentModificationException?

Guess you like

Origin blog.csdn.net/qq_41813208/article/details/107777539
Recommended