ArrayList
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
add method:
/** * 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; } private void ensureCapacityInternal(int minCapacity) { if(== elementData of DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { // the container is first added data, the initial value of the size of the capacity of the container is provided be minCapacity = Math.max (DEFAULT_CAPACITY, be minCapacity); } // The lower limit given size of the container, whether or not the expansion vessel ensureExplicitCapacity (be minCapacity); } Private void ensureExplicitCapacity ( int be minCapacity) { // Fail-FAST mechanism reference value ModCount ++ ; // overflow-Conscious code IF (be minCapacity - elementData.length> 0 ) // size of the container is insufficient, the expansion Grow ( be minCapacity); } / ** Increases The Capacity to Ensure® * that Least The IT CAN HOLD AT * Number of Elements The Minimum Capacity specified by argument. * * @Param be minCapacity The Desired Minimum Capacity * / Private void Grow ( int be minCapacity) { // overflow-Conscious code int = oldCapacity elementData.length; // every time expanded to 1.5 times the original size of the container int newCapacity = oldCapacity + (oldCapacity >>. 1 ); // fallback mechanism, the expanded size of the container, if the condition is not satisfied, then given container size IF (newCapacity - be minCapacity <0 ) newCapacity =be minCapacity; // if the size of the container exceeds the upper limit value after a predetermined expansion vessel, in both cases @ 1 exceeds the maximum size of the int, become negative, directly thrown @ 2 is the size of the container exceeds the upper limit value , but not exceeding the maximum value of the int, the size of the container is initialized to the maximum value of int IF (newCapacity - MAX_ARRAY_SIZE> 0 ) newCapacity = hugeCapacity (be minCapacity); // be minCapacity iS usually Close to size, the this SO iS a win: elementData of = Arrays.copyOf (elementData of, newCapacity); } Private static int hugeCapacity ( int be minCapacity) { IF (be minCapacity <0) // overflow the throw new new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; }
In short, it is a dynamically expansion array,
Look get method
/** * Returns the element at the specified position in this list. * * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E get(int index) { rangeCheck(index); return elementData(index); } /** * Checks if the given index is in range. If not, throws an appropriate * runtime exception. This method does *not* check if the index is * negative: It is always used immediately prior to an array access, * which throws an ArrayIndexOutOfBoundsException if index is negative. */ private void rangeCheck(int index) { if (index >= size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } // Positional Access Operations @SuppressWarnings("unchecked") E elementData(int index) { return (E) elementData[index]; }
First check whether the index is out of bounds, out of bounds exception is thrown, otherwise it returns data.
remove method:
/** * Removes the element at the specified position in this list. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param index the index of the element to be removed * @return the element that was removed from the list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E remove(int index) { //检查索引是否越界 rangeCheck(index); modCount++; //保留原有值,作为返回数据 OldValue = E elementData of (index); // calculate the need to move the array of data length int numMoved size = - index -. 1 ; IF (numMoved> 0 ) // all the index data following a forward movement System.arraycopy ( elementData of,. 1 + index , elementData of, index, numMoved); // delete the last redundant data array, to facilitate recovery gc elementData of [- size] = null ; // Clear the gC to the let do ITS Work return oldValue; }
Compared under both add and remove, add have found that although the expansion mechanism, but did not remove volume reduction mechanism. With the deletion of elements, but the corresponding element of the reference release, and not the array itself the capacity to adjust the size.