2 ArrayList Detailed

List is orderly, reusable containers. List Each element has index marks, access elements may be labeled according to the index of the element to precisely control these elements.

List Common Interface implementation class: ArrayList, LinkedList, Vector.

ArrayList underlying array is implemented. Features: high query efficiency, low efficiency additions and deletions, thread-safe. More deletions operation scenario using LinkedList, security threads using a thread-safe collection classes Vector or encapsulated.

 

1、ArrayList

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

ArrayList achieved by dynamic arrays, and achieves all of the optional list operations, allowing all the elements, including including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the internal array to store the list.

 

2, parsing source code

ArrayList is implemented by a dynamic array of objects, create an empty array default constructor. When the first additive element, the expansion capacity of 10, the expansion algorithm after the original capacity is 1.5 times.

(1) a method of adding

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

 

(2) Expansion Method

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

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

 

(3) Delete method

    public E remove(int index) {
        rangeCheck(index);

        modCount++;
        E oldValue = elementData(index);

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work

        return oldValue;
    }

 

3 ArrayList features

3.1 Dynamic arrays are not suitable for delete, insert operation.

3.2 In order to prevent excessive expansion of dynamic arrays, it is recommended given the initial capacity creation.

More than 3.3 thread-safe, suitable for use in single-threaded.

3.4 The recommended storage objects of the same type.

 

4 ArrayList extension method

addAll(Collection c)

removeAll(Collection c)

retainAll(Collection c)

containsALll(Collection c)

indexOf(Object o)

lastIndexOf(Object o)

 

 

Guess you like

Origin www.cnblogs.com/Latiny/p/10705934.html