ArrayList collection source

Posted by dr-dre67 on Mon, 06 Jan 2020 16:12:42 +0100

Based on JDK 1.7

Inheritance and implementation methods

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{

Let's look at the main methods add(E) to add elements

 /**
     * Appends the specified element to the end of this list.
     *
     * @param e element to be appended to this list
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     */
    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

arrayList is implemented internally based on arrays. Each element corresponds to a value in the array, so there are indexes 0, 1, 2
add method is not locked, and methods in ArrayList are not locked
Ensurcapacityinternal method

 private void ensureCapacityInternal(int minCapacity) {
        modCount++;
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

Ensure no array overrun Continue to see the grow method

 /**
     * 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); 
    }

The maximum number of elements stored in ArrayList is max? Array? Size, the maximum value of integer
Newcapacity = oldcapacity + (oldcapacity > > 1); this line of code sets the space size of the list to 1.5 times of the original size, and then continues to determine whether there is an overrun,

/**
     * The maximum size of array to allocate.
     * Some VMs reserve some header words in an array.
     * Attempts to allocate larger arrays may result in
     * OutOfMemoryError: Requested array size exceeds VM limit
     */
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

When the maximum storage space exceeds integer.max value, it returns Max array size. If it is less than 0, the subscript overflows.

private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

Conclusion:
1. ArrayList is actually the data stored in the array, with 0,1,2 Indexes;
2. The maximum storage is not infinite, and the size is max? Array? Size = integer.max? Value - 8;
3. Methods in ArrayList are not locked and are not thread safe.

Topics: JDK Java less