ArrayList source reading (added)

Keywords: Java Attribute

Version jdk1.7

  1. common method

increase

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

    ensureCapacityInternal(size + 1);  // Increments modCount!!
    System.arraycopy(elementData, index, elementData, index + 1,
                     size - index);
    elementData[index] = element;
    size++;
}
public boolean addAll(Collection<? extends E> c) {
    Object[] a = c.toArray();
    int numNew = a.length;
    ensureCapacityInternal(size + numNew);  // Increments modCount
    System.arraycopy(a, 0, elementData, size, numNew);
    size += numNew;
    return numNew != 0;
}
public boolean addAll(int index, Collection<? extends E> c) {
    rangeCheckForAdd(index);

    Object[] a = c.toArray();
    int numNew = a.length;
    ensureCapacityInternal(size + numNew);  // Increments modCount

    int numMoved = size - index;
    if (numMoved > 0)
        System.arraycopy(elementData, index, elementData, index + numNew,
                         numMoved);

    System.arraycopy(a, 0, elementData, index, numNew);
    size += numNew;
    return numNew != 0;
}

Capacity expansion algorithm

Now the capacity is expanded to 1.5 times of the original capacity. If it is larger than the demand capacity, the new capacity is 1.5 times of the original capacity, otherwise it is the demand capacity

private void ensureCapacityInternal(int minCapacity) {
    // If the container is empty, compare the initialization capacity with the required capacity, whichever is greater
    if (elementData == EMPTY_ELEMENTDATA) {
        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
    }

    ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
    modCount++;

    // overflow-conscious code
    // If the required capacity is greater than the current capacity, expand the capacity
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}
private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        // New capacity is 1.5 times the original capacity
        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);
    }

Let's talk about the two functions, Arrays.copy() and system. Array copy()

A native static method arraycopy() is provided in System, which can be used to copy arrays. For a one-dimensional array, the value of the copied attribute is passed, and the modified copy does not affect the original value. When objects are stored in two-dimensional or one-dimensional arrays, the replication result is that one-dimensional reference variables are passed to the one-dimensional array of the replica. When the replica is modified, the original array will be affected.

// Arrays.copy() the main class is an array of the same type, and then calls System.arraycopy().
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    T[] copy = ((Object)newType == (Object)Object[].class)
        ? (T[]) new Object[newLength]
        : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;
}

Posted by SnowControl on Sun, 08 Dec 2019 02:53:24 -0800