ArrayList是基於數組實現的,是一個動態數組,其容量能自動增加,相似於C語言中的動態申請內存,動態增加內存。javascript
ArrayList不是線程安全的,只能用在單線程環境下,多線程環境下能夠考慮用Collections.synchronizedList(List l)函數返回一個線程安全的ArrayList類,也可使用concurrent併發包下的CopyOnWriteArrayList類。java
ArrayList實現了Serializable接口,所以它支持序列化,可以經過序列化傳輸,實現了RandomAccess接口,支持快速隨機訪問,實際上就是經過下標序號進行快速訪問,實現了Cloneable接口,能被克隆。數組
ArrayList的源碼以下(加入了比較詳細的註釋):安全
package java.util;
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
// 序列版本號
private static final long serialVersionUID = 8683452581122892189L;
// ArrayList基於該數組實現,用該數組保存數據
private transient Object[] elementData;
// ArrayList中實際數據的數量
private int size;
// ArrayList帶容量大小的構造函數。
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
// 新建一個數組
this.elementData = new Object[initialCapacity];
}
// ArrayList無參構造函數。默認容量是10。
public ArrayList() {
this(10);
}
// 建立一個包含collection的ArrayList
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
// 將當前容量值設爲實際元素個數
public void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (size < oldCapacity) {
elementData = Arrays.copyOf(elementData, size);
}
}
// 肯定ArrarList的容量。
// 若ArrayList的容量不足以容納當前的所有元素,設置 新的容量=「(原始容量x3)/2 + 1」
public void ensureCapacity(int minCapacity) {
// 將「修改統計數」+1,該變量主要是用來實現fail-fast機制的
modCount++;
int oldCapacity = elementData.length;
// 若當前容量不足以容納當前的元素個數,設置 新的容量=「(原始容量x3)/2 + 1」
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
//若是還不夠,則直接將minCapacity設置爲當前容量
if (newCapacity < minCapacity)
newCapacity = minCapacity;
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
// 添加元素e
public boolean add(E e) {
// 肯定ArrayList的容量大小
ensureCapacity(size + 1); // Increments modCount!!
// 添加e到ArrayList中
elementData[size++] = e;
return true;
}
// 返回ArrayList的實際大小
public int size() {
return size;
}
// ArrayList是否包含Object(o)
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
//返回ArrayList是否爲空
public boolean isEmpty() {
return size == 0;
}
// 正向查找,返回元素的索引值
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;
}
// 反向查找,返回元素的索引值
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;
}
// 反向查找(從數組末尾向開始查找),返回元素(o)的索引值
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;
}
// 返回ArrayList的Object數組
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
// 返回ArrayList元素組成的數組
public <T> T[] toArray(T[] a) {
// 若數組a的大小 < ArrayList的元素個數;
// 則新建一個T[]數組,數組大小是「ArrayList的元素個數」,並將「ArrayList」所有拷貝到新數組中
if (a.length < size)
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
// 若數組a的大小 >= ArrayList的元素個數;
// 則將ArrayList的所有元素都拷貝到數組a中。
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
// 獲取index位置的元素值
public E get(int index) {
RangeCheck(index);
return (E) elementData[index];
}
// 設置index位置的值爲element
public E set(int index, E element) {
RangeCheck(index);
E oldValue = (E) elementData[index];
elementData[index] = element;
return oldValue;
}
// 將e添加到ArrayList中
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
// 將e添加到ArrayList的指定位置
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
// 刪除ArrayList指定位置的元素
public E remove(int index) {
RangeCheck(index);
modCount++;
E oldValue = (E) elementData[index];
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
// 刪除ArrayList的指定元素
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;
}
// 快速刪除第index個元素
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
// 從"index+1"開始,用後面的元素替換前面的元素。
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
// 將最後一個元素設爲null
elementData[--size] = null; // Let gc do its work
}
// 刪除元素
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
// 便利ArrayList,找到「元素o」,則刪除,並返回true。
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
// 清空ArrayList,將所有的元素設爲null
public void clear() {
modCount++;
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
// 將集合c追加到ArrayList中
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
// 從index位置開始,將集合c添加到ArrayList
public boolean addAll(int index, Collection<? extends E> c) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: " + index + ", Size: " + size);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(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;
}
// 刪除fromIndex到toIndex之間的所有元素。
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// Let gc do its work
int newSize = size - (toIndex-fromIndex);