1.ArrayList inheritance is as follows:

RandomAccess interface: a flag interface that indicates that the List collection that implements this interface supports fast RandomAccess. If it’s a List that implements this interface, using a for loop to get the data is faster than using an iterator.

2. Main member variables

// The actual number of elements

private int size;
Copy the code

// An array of objects

transient Object[] elementData;
Copy the code

// The default initialization capacity of the array is 10, the default initialization capacity of the constructor without arguments

private static final int DEFAULT_CAPACITY = 10;
Copy the code

// Two empty Object arrays

private static final Object[] EMPTY_ELEMENTDATA = {};

private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
Copy the code

The use of two empty arrays is used here to distinguish whether an ArrayList is constructed by a constructor with or without parameters. So that when you add elements to the collection for the first time, you know the corresponding expansion plan.

// An integer that records the number of operations on the collection

protected transient int modCount = 0;
Copy the code

3. Analysis of main methods

(1). The add method

EnsureCapacityInternal () is used to add elements to the collection. MinCapacity is the minimum number of elements currently in the collection size + 1. Let’s look at the method EnrecapityInternal ().

The static method calculateCapacity() is called to determine the minimum minCapacity required.

When we create a collection using the no-argument constructor, the default size of the array is 10. When we add elements to the collection for the first time, we expand the array to 10. MinCapacity is set to 10. MinCapacity is still size + 1.

The ensureExplicitCapacity() method is then called.

Determine whether the required minimum capacity is greater than the current array length, and if so, call the grow() method to expand.

OldCapacity is the current array length, new capacity

**                            newCapacity = oldCapacity + oldCapacity/2**

That is, the array capacity is increased by 1.5 times (rounded down). Assign the maximum value of newCapacity and minCapacity as the final capacity of the array to newCapacity. Then determine whether the final capacity expansion is greater than the maximum capacity defined by the collection, and if so, call the static method hugeCapacity().

Check whether minCapacity is greater than the maximum capacity defined by the set (integer Max. -8). If it is greater than newCapacity, the value is set to the maximum capacity defined by the set; otherwise, it is set to the maximum capacity defined by the set.

Then call arrays.copyof () for capacity expansion.

Underlying this method is a native method called System.arrayCopy () that eventually copies the array.

4. About looping to remove elements from list

(1). Plain for loop

When using the normal for loop to remove elements, be aware that the actual size of the collection is always changing, and your index is always changing, missing elements as you traverse.

(2). Enhance the for loop

This is essentially implemented through iterators, equivalent to the following code.

In the enhanced for loop in the loop to remove (or add) operation, will throw ConcurrentModificationException is unusual, because the remove and add operation will modify modCount values, in the next method of iterators, The checkForComodification() method is called each time to verify whether the current modCount value is the same as the expected value, so an exception is thrown.

(3). The iterator

Use iterator can do normal circulation and delete, but must use the remove method of iterators, otherwise will be throw ConcurrentModificationException anomalies.