以下源碼基于 Android SDK 23, 與JDK中略有差別,但基本相同;整體源碼由 構造、添加(add)、設置(set)、獲取(get)、移除(remove)、迭代器(iterator) 和序列化(Serializable)組成,最后我還會把里邊一些不常用的方法舉例說明下作用,下面我們就一一探究其實現原理。
概述
ArrayList
存儲的實質是操作一個數組,這個數組可以根據內容自動擴容,所以讓 ArrayList
看起來像一個無限大小的容器一樣。
屬性
/**
* The minimum amount by which the capacity of an ArrayList will increase.
* This tuning parameter controls a time-space tradeoff. This value (12)
* gives empirically good results and is arguably consistent with the
* RI's specified default initial capacity of 10: instead of 10, we start
* with 0 (sans allocation) and jump to 12.
*/
private static final int MIN_CAPACITY_INCREMENT = 12;
/**
* The number of elements in this list.
*/
int size;
/**
* The elements in this list, followed by nulls.
*/
transient Object[] array;
- 既然
ArrayList
可以自動擴容,那么就要有一個描述每次擴容的基準,MIN_CAPACITY_INCREMENT
就是這個基準,默認值是12。 -
array
是ArrayList
的核心,所有數據均存儲在array
這個數組中,發生自動擴容時,array
會指向新的數組首地址,但注意了,transient
表示它不會參與序列化過程。 -
size
始終描述ArrayList
中實際的大小。
構造方法
/**
* Constructs a new instance of {@code ArrayList} with the specified
* initial capacity.
*
* @param capacity
* the initial capacity of this {@code ArrayList}.
*/
public ArrayList(int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("capacity < 0: " + capacity);
}
array = (capacity == 0 ? EmptyArray.OBJECT : new Object[capacity]);
}
/**
* Constructs a new {@code ArrayList} instance with zero initial capacity.
*/
public ArrayList() {
array = EmptyArray.OBJECT;
}
/**
* Constructs a new instance of {@code ArrayList} containing the elements of
* the specified collection.
*
* @param collection
* the collection of elements to add.
*/
public ArrayList(Collection<? extends E> collection) {
if (collection == null) {
throw new NullPointerException("collection == null");
}
Object[] a = collection.toArray();
if (a.getClass() != Object[].class) {
Object[] newArray = new Object[a.length];
System.arraycopy(a, 0, newArray, 0, a.length);
a = newArray;
}
array = a;
size = a.length;
}
ArrayList
共含有3個構造方法,EmptyArray.OBJECT
是一個length為0的空數組new Object[0]
,new ArrayList() 則會創建一個大小為0的數組;你也可以去指定初始的容量capacity
,new ArrayList(int capacity) ,避免ArrayList
第一次add 或者其他操作就進行擴容;第三個構造可以傳入一個集合,這里要提一下Collection
,你可以認為它是 List
、Queue
、Set
的始祖,這里只要在它們內部實現了 toArray
方法,并且返回一個Object[]類型的數據,就可以成功初始化到 ArrayList中。
添加(add / addAll)
/**
* Adds the specified object at the end of this {@code ArrayList}.
*
* @param object
* the object to add.
* @return always true
*/
@Override public boolean add(E object) {
Object[] a = array;
int s = size;
if (s == a.length) {
Object[] newArray = new Object[s +
(s < (MIN_CAPACITY_INCREMENT / 2) ?
MIN_CAPACITY_INCREMENT : s >> 1)];
System.arraycopy(a, 0, newArray, 0, s);
array = a = newArray;
}
a[s] = object;
size = s + 1;
modCount++;
return true;
}
/**
* Inserts the specified object into this {@code ArrayList} at the specified
* location. The object is inserted before any previous element at the
* specified location. If the location is equal to the size of this
* {@code ArrayList}, the object is added at the end.
*
* @param index
* the index at which to insert the object.
* @param object
* the object to add.
* @throws IndexOutOfBoundsException
* when {@code location < 0 || location > size()}
*/
@Override public void add(int index, E object) {
Object[] a = array;
int s = size;
if (index > s || index < 0) {
throwIndexOutOfBoundsException(index, s);
}
if (s < a.length) {
System.arraycopy(a, index, a, index + 1, s - index);
} else {
// assert s == a.length;
Object[] newArray = new Object[newCapacity(s)];
System.arraycopy(a, 0, newArray, 0, index);
System.arraycopy(a, index, newArray, index + 1, s - index);
array = a = newArray;
}
a[index] = object;
size = s + 1;
modCount++;
}
/**
* This method controls the growth of ArrayList capacities. It represents
* a time-space tradeoff: we don't want to grow lists too frequently
* (which wastes time and fragments storage), but we don't want to waste
* too much space in unused excess capacity.
*
* NOTE: This method is inlined into {@link #add(Object)} for performance.
* If you change the method, change it there too!
*/
private static int newCapacity(int currentCapacity) {
int increment = (currentCapacity < (MIN_CAPACITY_INCREMENT / 2) ?
MIN_CAPACITY_INCREMENT : currentCapacity >> 1);
return currentCapacity + increment;
}
/**
* Adds the objects in the specified collection to this {@code ArrayList}.
*
* @param collection
* the collection of objects.
* @return {@code true} if this {@code ArrayList} is modified, {@code false}
* otherwise.
*/
@Override public boolean addAll(Collection<? extends E> collection) {
Object[] newPart = collection.toArray();
int newPartSize = newPart.length;
if (newPartSize == 0) {
return false;
}
Object[] a = array;
int s = size;
int newSize = s + newPartSize; // If add overflows, arraycopy will fail
if (newSize > a.length) {
int newCapacity = newCapacity(newSize - 1); // ~33% growth room
Object[] newArray = new Object[newCapacity];
System.arraycopy(a, 0, newArray, 0, s);
array = a = newArray;
}
System.arraycopy(newPart, 0, a, s, newPartSize);
size = newSize;
modCount++;
return true;
}
/**
* Inserts the objects in the specified collection at the specified location
* in this List. The objects are added in the order they are returned from
* the collection's iterator.
*
* @param index
* the index at which to insert.
* @param collection
* the collection of objects.
* @return {@code true} if this {@code ArrayList} is modified, {@code false}
* otherwise.
* @throws IndexOutOfBoundsException
* when {@code location < 0 || location > size()}
*/
@Override
public boolean addAll(int index, Collection<? extends E> collection) {
int s = size;
if (index > s || index < 0) {
throwIndexOutOfBoundsException(index, s);
}
Object[] newPart = collection.toArray();
int newPartSize = newPart.length;
if (newPartSize == 0) {
return false;
}
Object[] a = array;
int newSize = s + newPartSize; // If add overflows, arraycopy will fail
if (newSize <= a.length) {
System.arraycopy(a, index, a, index + newPartSize, s - index);
} else {
int newCapacity = newCapacity(newSize - 1); // ~33% growth room
Object[] newArray = new Object[newCapacity];
System.arraycopy(a, 0, newArray, 0, index);
System.arraycopy(a, index, newArray, index + newPartSize, s-index);
array = a = newArray;
}
System.arraycopy(newPart, 0, a, index, newPartSize);
size = newSize;
modCount++;
return true;
}
這里有必要先看一個方法,System.arraycopy()
public static native void arraycopy(Object src, int srcPos,
Object dst, int dstPos, int length);
這是一個 native方法,負責數組拷貝,從 src
的 srcPos
開始,將 length
長度的數據拷貝到 dst
中,dstPos
中的數據是srcPos
位置的數據。
public boolean add(E object)
這是最簡單的一個add操作,里邊會進行擴容判斷,如果當前ArrayList.size
與array.length
相同,則進行擴容,擴容的策略是s < (MIN_CAPACITY_INCREMENT / 2) ? MIN_CAPACITY_INCREMENT : s >> 1
,即 s < 6 ? 6 : s * 2, 最終擴容的大小為 (s + s < 6 ? 6 : s * 2);newCapacity(int currentCapacity)
方法也是這個作用,返回最終擴容后的大小。
public void add(int index, E object)
這個方法的作用是將 object
插入至 index
位置,這里也會有擴容判斷,既然是插入一個值,那么size
就會 +1,所以 ArrayList.size
小于 array.length
是一種情況,數組可以直接從 index處 后移一位,再將 object
放入 index
的位置;若是大于等于,則原array需要擴容,擴容后現將old array
數據 復制到 new array
中,再進行后移,最終把object
插入到index
位置。
public boolean addAll(Collection<? extends E> collection)
public boolean addAll(int index, Collection<? extends E> collection)
這兩個方法只是批量操作,內部邏輯與add
是一樣的,都要先判斷 ArrayList.size
與array.length
的大小關系進行擴容,之后通過 System.arraycopy
去操作array
。
注:這里你有可能會發現有個變量
modCount
,它用來表達ArrayList
的修改次數(add、remove),是它導致ArrayList
不是線程安全的,等講到迭代器iterator
的時候再來說說這個變量。
設置
/**
* Replaces the element at the specified location in this {@code ArrayList}
* with the specified object.
*
* @param index
* the index at which to put the specified object.
* @param object
* the object to add.
* @return the previous element at the index.
* @throws IndexOutOfBoundsException
* when {@code location < 0 || location >= size()}
*/
@Override public E set(int index, E object) {
Object[] a = array;
if (index >= size) {
throwIndexOutOfBoundsException(index, size);
}
@SuppressWarnings("unchecked") E result = (E) a[index];
a[index] = object;
return result;
}
這個方法沒什么,就是把array[index]
替換,并且把原來的數據返回。
獲取
@SuppressWarnings("unchecked")
@Override
public E get(int index) {
if (index >= size) {
throwIndexOutOfBoundsException(index, size);
}
return (E) array[index];
}
這個方法也不多說,將array[index]
返回。
移除
/**
* Removes the object at the specified location from this list.
*
* @param index
* the index of the object to remove.
* @return the removed object.
* @throws IndexOutOfBoundsException
* when {@code location < 0 || location >= size()}
*/
@Override public E remove(int index) {
Object[] a = array;
int s = size;
if (index >= s) {
throwIndexOutOfBoundsException(index, s);
}
@SuppressWarnings("unchecked") E result = (E) a[index];
System.arraycopy(a, index + 1, a, index, --s - index);
a[s] = null; // Prevent memory leak
size = s;
modCount++;
return result;
}
@Override public boolean remove(Object object) {
Object[] a = array;
int s = size;
if (object != null) {
for (int i = 0; i < s; i++) {
if (object.equals(a[i])) {
System.arraycopy(a, i + 1, a, i, --s - i);
a[s] = null; // Prevent memory leak
size = s;
modCount++;
return true;
}
}
} else {
for (int i = 0; i < s; i++) {
if (a[i] == null) {
System.arraycopy(a, i + 1, a, i, --s - i);
a[s] = null; // Prevent memory leak
size = s;
modCount++;
return true;
}
}
}
return false;
}
@Override protected void removeRange(int fromIndex, int toIndex) {
if (fromIndex == toIndex) {
return;
}
Object[] a = array;
int s = size;
if (fromIndex >= s) {
throw new IndexOutOfBoundsException("fromIndex " + fromIndex
+ " >= size " + size);
}
if (toIndex > s) {
throw new IndexOutOfBoundsException("toIndex " + toIndex
+ " > size " + size);
}
if (fromIndex > toIndex) {
throw new IndexOutOfBoundsException("fromIndex " + fromIndex
+ " > toIndex " + toIndex);
}
System.arraycopy(a, toIndex, a, fromIndex, s - toIndex);
int rangeSize = toIndex - fromIndex;
Arrays.fill(a, s - rangeSize, s, null);
size = s - rangeSize;
modCount++;
}
add
方法已經進行了詳細的講解,想必大家都能猜到,remove
操作就是講 index
或者 range
的一段數據從array
中移除,然后再通過System.arraycopy
拷貝之后的數據前移補充空位,下圖以移除單個為例,將步驟分解:
迭代器
@Override public Iterator<E> iterator() {
return new ArrayListIterator();
}
private class ArrayListIterator implements Iterator<E> {
/** Number of elements remaining in this iteration */
private int remaining = size;
/** Index of element that remove() would remove, or -1 if no such elt */
private int removalIndex = -1;
/** The expected modCount value */
private int expectedModCount = modCount;
public boolean hasNext() {
return remaining != 0;
}
@SuppressWarnings("unchecked") public E next() {
ArrayList<E> ourList = ArrayList.this;
int rem = remaining;
if (ourList.modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (rem == 0) {
throw new NoSuchElementException();
}
remaining = rem - 1;
return (E) ourList.array[removalIndex = ourList.size - rem];
}
public void remove() {
Object[] a = array;
int removalIdx = removalIndex;
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (removalIdx < 0) {
throw new IllegalStateException();
}
System.arraycopy(a, removalIdx + 1, a, removalIdx, remaining);
a[--size] = null; // Prevent memory leak
removalIndex = -1;
expectedModCount = ++modCount;
}
}
迭代器,一個很重要的概念,它的作用就是便利整個ArrayList
, for each 的原理其實就是迭代器的使用,上文說到了modCount
與迭代器相關,
if (ourList.modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
expectedModCount
是iterator
初始化時賦予的值,值為modCount
,而modCount
會根據add
或者remove
進行++操作,這就表明,當iterator
創建好后,只要使用這個iterator
實例去進行遍歷,就不能使用ArrayList.add
或者ArrayList.remove
操作,因為如果使用了,modCount
會發生變化,這樣在next()
的時候就會拋出異常ConcurrentModificationException
,這也進一步說明ArrayList
不是線程安全的。那么在遍歷中如何移除元素呢,就是下邊實現的remove
方法了,remove過程與之前類似,關鍵在于expectedModCount = ++modCount;
,remove
需要使modCount
遞增,那么我讓expectedModCount
重新賦值,即可完成刪除操作。
序列化
private static final long serialVersionUID = 8683452581122892189L;
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
stream.writeInt(array.length);
for (int i = 0; i < size; i++) {
stream.writeObject(array[i]);
}
}
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
int cap = stream.readInt();
if (cap < size) {
throw new InvalidObjectException(
"Capacity: " + cap + " < size: " + size);
}
array = (cap == 0 ? EmptyArray.OBJECT : new Object[cap]);
for (int i = 0; i < size; i++) {
array[i] = stream.readObject();
}
}
這是在代碼末尾了,ArrayList
是通過stream.writeObject
連續寫入 array
的內容。
其他
public boolean contains(Object object)
利用 object
的 equals方法判斷ArrayList
中是否包含object
對象。
public int indexOf(Object object)
public int lastIndexOf(Object object)
這兩個方法都是獲取 object
在 ArrayList
中的位置,第一個是正序遍歷,找到的第一個返回的index
;第二個是倒序遍歷,找到第一個返回的index
。
/**
* Sets the capacity of this {@code ArrayList} to be the same as the current
* size.
*
* @see #size
*/
public void trimToSize() {
int s = size;
if (s == array.length) {
return;
}
if (s == 0) {
array = EmptyArray.OBJECT;
} else {
Object[] newArray = new Object[s];
System.arraycopy(array, 0, newArray, 0, s);
array = newArray;
}
modCount++;
}
這個方法是將當前的array
“精簡”一下,比如 array.length 是10,但里邊的size是 5個,那么就將 array.length變為 5,把數據通過 System.arraycopy 拷貝到新的 array中。
@Override public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof List)) {
return false;
}
List<?> that = (List<?>) o;
int s = size;
if (that.size() != s) {
return false;
}
Object[] a = array;
if (that instanceof RandomAccess) {
for (int i = 0; i < s; i++) {
Object eThis = a[i];
Object ethat = that.get(i);
if (eThis == null ? ethat != null : !eThis.equals(ethat)) {
return false;
}
}
} else { // Argument list is not random access; use its iterator
Iterator<?> it = that.iterator();
for (int i = 0; i < s; i++) {
Object eThis = a[i];
Object eThat = it.next();
if (eThis == null ? eThat != null : !eThis.equals(eThat)) {
return false;
}
}
}
return true;
}
再來看下這個長長的equals
方法,非常好懂,但是乍眼一看有個 RandomAccess
,這是什么?尋找了一下它的實現類,發現ArrayList
就是它的實現類,再看下這個if(...){}else{}
,如果是RandomAccess
的實現類,那么直接使用get(index)
獲取元素,否則需要使用迭代器iterator
。以下是對于RandomAccess
的一段摘錄:
jdk中有個RandomAccess接口,這是一個標記接口(Marker),它沒有任何方法,這個接口被List的實現類(子類)使用。如果List子類實現了RandomAccess接口,那就表示它能夠快速隨機訪問存儲的元素。RandomAccess接口的意義在于:在對列表進行隨機或順序訪問的時候,訪問算法能夠選擇性能最佳方式。一般的列表訪問算法在訪問列表元素之前,都被建議先使用instanceof關鍵字檢查一下列表是否是一個RandomAccess子類,然后再決定采用隨機還是順序方式訪問列表中的元素,這樣可以保證訪問算法擁有最佳的性能。對于List的子類,如果:
for (int i=0, n=list.size(); i < n; i++)
list.get(i);
的訪問方式比
for (Iterator i=list.iterator(); i.hasNext(); )
i.next();
快,那么它應該實現RandomAccess接口。