源码版本为JDK1.7.0_79

ArrayList不是线程安全的,只能应用在单线程环境下。

ArrayList类定义

ArrayList的类定义可以看出它是支持泛型的,继承自AbstractListAbstractList 实现了List接口,提供了List接口的默认实现。

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>

ArrayList自身也实现了List接口。同时,ArrayList实现了Serializable接口,因此它支持序列化,能够通过序列化传输。实现了RandomAccess接口,支持快速随机访问,实际上就是通过下标进行快速访问,RandomAccess是一个标记接口,接口内没有定义任何内容。实现了Cloneable接口,能被克隆。

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

ArrayList类属性

ArrayList是基于数组实现的,是一个动态数组,其容量能够自动增长。

/**
* 序列版本号
*/
private static final long serialVersionUID = 8683452581122892189L;

/**
* Default initial capacity. 
*/
private static final int DEFAULT_CAPACITY = 10;

/**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {};

/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to
* DEFAULT_CAPACITY when the first element is added.
*/
private transient Object[] elementData;

/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;

数组elementDataArrayList存储元素的BufferArrayList的容量即为该数组的长度。当ArrayList为空时,此时elementData==EMPTY_ELEMENTDATA,当往ArrayList添加一个元素时,elementData的长度就被初始化为10,也就是DEFAULT_CAPACITY的值。

sizeelementData含有的元素的个数。

注意到elementData数组被transient修饰,因为ArrayList实现了Serializable接口,这意味着ArrayList可以被序列化,也就是说,ArrayList的属性可以通过网络传输,或者被存储到磁盘持久化。但是在很多情况下,我们并不希望有些属性被传输或者存储,比如一些敏感信息。这时使用trainsent标记属性,就可以使得ArrayList在序列化的过程中elementData不参与序列化。因此,elementData中的数据仅存在于调用者的内存中。

ArrayList类构造函数

ArrayList一共定义了三个构造函数。

/**
* Constructs an empty list with the specified initial capacity.
*/
public ArrayList(int initialCapacity) {
    super();
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal Capacity: "+
                                          initialCapacity);
    this.elementData = new Object[initialCapacity];
}

/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
    super();
    this.elementData = EMPTY_ELEMENTDATA;
}

/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*/
public ArrayList(Collection<? extends E> c) {
    elementData = c.toArray();
    size = elementData.length;
    // c.toArray might (incorrectly) not return Object[] (see 6260652)
    if (elementData.getClass() != Object[].class)
        elementData = Arrays.copyOf(elementData, size, Object[].class);
}

构造函数public ArrayList(int initialCapacity){...}为构造给定大小的elementData数组。

构造函数public ArrayList() {...}ArrayList的默认构造函数,此时elementData==EMPTY_ELEMENTDATA,数组长度为0

构造函数 public ArrayList(Collection<? extends E> c) {...}使用一个集合来初始化ArrayList

ArrayList类核心方法

/**
* Trims the capacity of this <tt>ArrayList</tt> instance to be the
* list's current size.  An application can use this operation to minimize
* the storage of an <tt>ArrayList</tt> instance.
*/
public void trimToSize() {
    modCount++;
    if (size < elementData.length) {
        elementData = Arrays.copyOf(elementData, size);
    }
}

modCountArrayList的父类AbstractList中定义的属性,其作用是 the number of times this list has been structurally modified

trimToSize的作用是将ArrayList对象的capacity减小到size长度,这样可以减少ArrayList对象占用的内存。

/**
* Increases the capacity of this <tt>ArrayList</tt> instance, if
* necessary, to ensure that it can hold at least the number of elements
* specified by the minimum capacity argument.
*/
public void ensureCapacity(int minCapacity) {
    int minExpand = (elementData != EMPTY_ELEMENTDATA)
        // any size if real element table
        ? 0
        // larger than default for empty table. It's already supposed to be
        // at default size.
        : DEFAULT_CAPACITY;

    if (minCapacity > minExpand) {
        ensureExplicitCapacity(minCapacity);
    }
}

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

    ensureExplicitCapacity(minCapacity);
}

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

    // overflow-conscious code
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}

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

/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*/
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);
}

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

oldCapacity + (oldCapacity >> 1)确保了一旦扩充容量,那么至少扩充到原来的1.5倍。

/**
* Returns the number of elements in this list.
*/
public int size() {
    return size;
}

/**
* Returns <tt>true</tt> if this list contains no elements.
*/
public boolean isEmpty() {
    return size == 0;
}

size方法和isEmpty方法都是根据属性size判断。

/**
* Returns <tt>true</tt> if this list contains the specified element.
* More formally, returns <tt>true</tt> if and only if this list contains
* at least one element <tt>e</tt> such that
* <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
*/
public boolean contains(Object o) {
    return indexOf(o) >= 0;
}

/**
* Returns the index of the first occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the lowest index <tt>i</tt> such that
* <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
* or -1 if there is no such index.
*/
public int indexOf(Object o) {
    if (o == null) {
        for (int i = 0; i < size; i++)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = 0; i < size; i++)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}

contains方法中,通过indexOf方法遍历elementData数组。若待搜索的元素为null,那么就返回elementData中第一个为null的元素的下标。当然如果elementDatasize==0,那么就返回-1。若待搜索的元素不为空,那么就返回对应的元素的下标,否则返回-1。注意当带搜索的元素不为null时,是使用equals方法来判断是否相等。

/**
* Returns the index of the last occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the highest index <tt>i</tt> such that
* <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
* or -1 if there is no such index.
*/
public int lastIndexOf(Object o) {
    if (o == null) {
        for (int i = size-1; i >= 0; i--)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = size-1; i >= 0; i--)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}

lastIndexOf方法与indexOf方法十分类似,只是它是从最后一个元素开始向前遍历。

/**
* Returns a shallow copy of this <tt>ArrayList</tt> instance.  (The
* elements themselves are not copied.)
*/
public Object clone() {
    try {
        @SuppressWarnings("unchecked")
            ArrayList<E> v = (ArrayList<E>) super.clone();
        v.elementData = Arrays.copyOf(elementData, size);
        v.modCount = 0;
        return v;
    } catch (CloneNotSupportedException e) {
        // this shouldn't happen, since we are Cloneable
        throw new InternalError();
    }
}

clone方法首先调用父类的clone方法返回一个对象的副本,将这个副本对象的elementData数组地址赋值为原来对象的elementData数组的地址,并将该副本对象的modCount重置为0。该方法返回的是ArrayList对象的浅副本,即不复制这些元素本身。

/**
* Returns an array containing all of the elements in this list
* in proper sequence (from first to last element).
*
* <p>The returned array will be "safe" in that no references to it are
* maintained by this list.  (In other words, this method must allocate
* a new array).  The caller is thus free to modify the returned array.
*
* <p>This method acts as bridge between array-based and collection-based
* APIs.
*
*/
public Object[] toArray() {
    return Arrays.copyOf(elementData, size);
}

/**
* Returns an array containing all of the elements in this list in proper
* sequence (from first to last element); the runtime type of the returned
* array is that of the specified array.  If the list fits in the
* specified array, it is returned therein.  Otherwise, a new array is
* allocated with the runtime type of the specified array and the size of
* this list.
*
* <p>If the list fits in the specified array with room to spare
* (i.e., the array has more elements than the list), the element in
* the array immediately following the end of the collection is set to
* <tt>null</tt>.  (This is useful in determining the length of the
* list <i>only</i> if the caller knows that the list does not contain
* any null elements.)
*/
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
    if (a.length < size)
        // Make a new array of a's runtime type, but my contents:
        return (T[]) Arrays.copyOf(elementData, size, a.getClass());
    System.arraycopy(elementData, 0, a, 0, size);
    if (a.length > size)
        a[size] = null;
    return a;
}

toArray方法返回的是elementData数组的副本,而不是ArrayList对象内的elementData数组本身。

@SuppressWarnings("unchecked")
E elementData(int index) {
    return (E) elementData[index];
}

elementData方法返回指定下标的元素,ArrayListget方法,set方法,add方法,remove方法和clear方法都需要调用它。

/**
* Returns the element at the specified position in this list.
*/
public E get(int index) {
    rangeCheck(index);

    return elementData(index);
}

get方法首先需要校验下标是否越界,若没有,则返回对应下标的元素。

/**
* Replaces the element at the specified position in this list with
* the specified element.
*/
public E set(int index, E element) {
    rangeCheck(index);

    E oldValue = elementData(index);
    elementData[index] = element;
    return oldValue;
}

set方法将ArrayList指定下标的元素设置为指定的值,并将原值作为返回值返回。

/**
* Appends the specified element to the end of this list.
*/
public boolean add(E e) {
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}

/**
* Inserts the specified element at the specified position in this
* list. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
*/
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++;
}

add(int index, E element)方法在指定的位置插入一个元素,首先将elementData中从index开始的 size - index 个元素整体向后移动一位,然后将element元素插入到index处。

/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts one from their
* indices).
*/
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;
}

如果要remove的元素为elementData的最后一个元素,那么numMoved等于0,因此不需要做数组拷贝的操作。若要删除的元素位于elementData之间,那么需要将index后的元素整体向前移动一位,并将elementData最后的元素置为null值让GC回收。

/**
* Removes the first occurrence of the specified element from this list,
* if it is present.  If the list does not contain the element, it is
* unchanged.  More formally, removes the element with the lowest index
* <tt>i</tt> such that
* <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
* (if such an element exists).  Returns <tt>true</tt> if this list
* contained the specified element (or equivalently, if this list
* changed as a result of the call).
*/
public boolean remove(Object o) {
    if (o == null) {
        for (int index = 0; index < size; index++)
            if (elementData[index] == null) {
                fastRemove(index);
                return true;
            }
    } else {
        for (int index = 0; index < size; index++)
            if (o.equals(elementData[index])) {
                fastRemove(index);
                return true;
            }
    }
    return false;
}

/*
* Private remove method that skips bounds checking and does not
* return the value removed.
*/
private void fastRemove(int index) {
    modCount++;
    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
}

fastRemoveremove非常类似,仅仅是移除某个元素。remove(Object o)是将elementData中的某个对象移除(假设存在),若传入的要移除的元素为null,那么移除elementData中首个为null的元素,返回true。若传入的要移除的元素不为null,遍历elementData,找到要移除的元素并移除。若要移除的元素并不存在于elementData,那么返回false

/**
* Appends all of the elements in the specified collection to the end of
* this list, in the order that they are returned by the
* specified collection's Iterator.  The behavior of this operation is
* undefined if the specified collection is modified while the operation
* is in progress.  (This implies that the behavior of this call is
* undefined if the specified collection is this list, and this
* list is nonempty.)
*
*/
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;
}

/**
* Inserts all of the elements in the specified collection into this
* list, starting at the specified position.  Shifts the element
* currently at that position (if any) and any subsequent elements to
* the right (increases their indices).  The new elements will appear
* in the list in the order that they are returned by the
* specified collection's iterator.
*
*/
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;
}

addAll(Collection<? extends E> c)是将集合c追加到elementData尾部,addAll(int index, Collection<? extends E> c)是将集合c插入到指定位置。若指定的indexelementDatasize处,那么直接将c追加到elementData尾部。否则,需要先将elementData中以index下标开始的numMoved个元素后移一位,然后把c插入index处。

/**
* Removes from this list all of the elements whose index is between
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
* Shifts any succeeding elements to the left (reduces their index).
* This call shortens the list by {@code (toIndex - fromIndex)} elements.
* (If {@code toIndex==fromIndex}, this operation has no effect.)
*/
protected void removeRange(int fromIndex, int toIndex) {
    modCount++;
    int numMoved = size - toIndex;
    System.arraycopy(elementData, toIndex, elementData, fromIndex,
                    numMoved);

    // clear to let GC do its work
    int newSize = size - (toIndex-fromIndex);
    for (int i = newSize; i < size; i++) {
        elementData[i] = null;
    }
    size = newSize;
}

removeRange将指定范围内的元素移除,首先将toIndex之后的元素拷贝到fromIndex之后,调整elementDatasizenewSize,然后将newSize之后的元素全部置空供GC回收。

/**
* Removes from this list all of its elements that are contained in the
* specified collection.
*/
public boolean removeAll(Collection<?> c) {
    return batchRemove(c, false);
}

/**
* Retains only the elements in this list that are contained in the
* specified collection.  In other words, removes from this list all
* of its elements that are not contained in the specified collection.
*/
public boolean retainAll(Collection<?> c) {
    return batchRemove(c, true);
}

private boolean batchRemove(Collection<?> c, boolean complement) {
    final Object[] elementData = this.elementData;
    int r = 0, w = 0;
    boolean modified = false;
    try {
        for (; r < size; r++)
            if (c.contains(elementData[r]) == complement)
                elementData[w++] = elementData[r];
    } finally {
        // Preserve behavioral compatibility with AbstractCollection,
        // even if c.contains() throws.
        if (r != size) {
            System.arraycopy(elementData, r,
                            elementData, w,
                            size - r);
            w += size - r;
        }
        if (w != size) {
            // clear to let GC do its work
            for (int i = w; i < size; i++)
                elementData[i] = null;
            modCount += size - w;
            size = w;
            modified = true;
        }
    }
    return modified;
}

removeAll的作用是删除elemetData中与指定集合c相同的元素。retainAll的作用是保留elementData中与指定集合c相同的元素,删除其他的元素。removeAllretainAll传入一个boolean类型的complement,在batchRemove中通过if (c.contains(elementData[r]) == complement) 来决定是否保留元素。removeAll传递过来的complementfalse,因此当elementData中的元素不包含在c中时,保留这个元素,达到了remove的效果。retainAll传递过来的complementtrue,因此当elementData中的元素包含在c中,保留这个元素,达到了retain的效果。

总结

注意ArrayList每次在增加元素前都需要调用ensureCapacityInternal来确保elementData的容量足够,当容量不够时,调用ensureExplicitCapacityensureExplicitCapacity调用grow方法线将容量增加到原来容量的1.5倍,若果增加为原来的1.5倍之后容量还不够,那么就把新容量直接设置为传递过来的容量(minCapacity)。然后调用Arrays.copyOf将原来的元素拷贝到新数组中。可以看出,当容量不够时,增加元素变成了一个非常耗时的过程。因此建议在实现能够确定元素数量的情况下才使用ArrayList,否则更加建议使用LinkedList

ArrayList的源码中多次调用了两个数组拷贝的方法,分别是Arrays.copyOfSystem.arraycopy,查看这两个方法的源码。

public static <T> T[] copyOf(T[] original, int newLength) {
    return (T[]) copyOf(original, newLength, original.getClass());
}

copyOf方法调用了另一个copyOf方法:

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

该方法创建了一个新的数组,然后调用System.arraycopy将元素拷贝到新数组中。

public static native void arraycopy(Object src,  int  srcPos,
                                    Object dest, int destPos,
                                    int length);

arraycopy是一个本地方法,通过调用系统的C/C++方法实现。该方法可以保证在同一个数组内的元素的复制和移动。

通过源码的分析,可以更加深刻的理解为什么说ArrayList的查找效率高而增加、删除元素的效率低。