學(xué)習(xí)源碼从橘,應(yīng)該是一件認(rèn)真與鉆研的功課,點(diǎn)滴積累。
package java.util;
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
//
private static final long serialVersionUID = 8683452581122892189L;
//
private static final int DEFAULT_CAPACITY = 10;
//
private static final Object[] EMPTY_ELEMENTDATA = {};
//
private transient Object[] elementData;
//
private int size;
/**************** Constructor ***********************/
//
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
//
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
//
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
/******************* Array size *************/
//
public void trimToSize() {
modCount++;
//
if (size < elementData.length) {
elementData = Arrays.copyOf(elementData, size);
}
}
//
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++; //
//
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
//
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
//
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;
}
//
public int size() {
return size;
}
//
public boolean isEmpty() {
return size == 0;
}
/****************************** Search Operations *************************/
//
public boolean contains(Object o) {
return indexOf(o) >= 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;
}
/******************************* Clone *********************************/
//克隆函數(shù)
public Object clone() {
try {
@SuppressWarnings("unchecked")
ArrayList<E> v = (ArrayList<E>) super.clone();
//將當(dāng)前ArrayList的全部元素拷貝到v中
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();
}
}
/********************************* toArray *****************************/
/**
* 返回一個(gè)Object數(shù)組派继,包含ArrayList中所有的元素
* toArray()方法扮演著array-based和collection-based API之間的橋梁
*/
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
//返回ArrayList的模板數(shù)組
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
//如果數(shù)組a的大小 < ArrayList的元素個(gè)數(shù),
//則新建一個(gè)T[]數(shù)組捻艳,大小為ArrayList元素個(gè)數(shù)驾窟,并將“ArrayList”全部拷貝到新數(shù)組中。
if (a.length < size)
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
//如果數(shù)組a的大小 >= ArrayList的元素個(gè)數(shù)认轨,
//則將ArrayList全部拷貝到新數(shù)組a中绅络。
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
/******************** Positional Access Operations ********************/
@SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}
//獲取index位置的元素值
public E get(int index) {
rangeCheck(index); //首先判斷index的范圍是否合法
return elementData(index);
}
//將index位置的值設(shè)為element,并返回原來(lái)的值
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
//將e添加到ArrayList中
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
//將element添加到ArrayList的指定位置
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1); // Increments modCount!!
//將index以及index之后的數(shù)據(jù)復(fù)制到index+1的位置往后嘁字,即從index開(kāi)始向后挪了一位
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element; //然后在index處插入element
size++;
}
//刪除ArrayList指定位置的元素
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
//向左挪一位恩急,index位置原來(lái)的數(shù)據(jù)已經(jīng)被覆蓋了
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
//多出來(lái)的最后一位刪掉
elementData[--size] = null; // clear to 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;
}
//private的快速刪除與上面的public普通刪除區(qū)別在于,沒(méi)有進(jìn)行邊界判斷以及不返回刪除值
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
}
//清空ArrayList纪蜒,將全部元素置為null
public void clear() {
modCount++;
// clear to let GC do its work
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;
ensureCapacityInternal(size + numNew); // Increments modCount
//在原來(lái)數(shù)組的后面添加c中所有的元素
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
//從index位置開(kāi)始衷恭,將集合C中所欲的元素添加到ArrayList中
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)
//將index開(kāi)始向后的所有數(shù)據(jù),向后移動(dòng)numNew個(gè)位置纯续,給新插入的數(shù)據(jù)騰出空間
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
//將集合C中的數(shù)據(jù)插到剛剛騰出的位置
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
//刪除從fromIndex到toIndex之間的數(shù)據(jù)随珠,不包括toIndex位置的數(shù)據(jù)
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;
}
//范圍檢測(cè)
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
//add和addAll方法中的范圍檢測(cè)
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
//刪除ArrayList中所有集合C中包含的數(shù)據(jù)
public boolean removeAll(Collection<?> c) {
return batchRemove(c, false);
}
//刪除ArrayList中除了集合C中包含的數(shù)據(jù)外的其他所有數(shù)據(jù)
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.
//官方的注釋是為了保持和AbstractCollection的兼容性
//我的理解是上面c.contains拋出了異常,導(dǎo)致for循環(huán)終止杆烁,那么必然會(huì)導(dǎo)致r != size
//所以0-w之間是需要保留的數(shù)據(jù)牙丽,同時(shí)從w索引開(kāi)始將剩下沒(méi)有循環(huán)的數(shù)據(jù)(也就是從r開(kāi)始的)拷貝回來(lái),也保留
if (r != size) {
System.arraycopy(elementData, r,
elementData, w,
size - r);
w += size - r;
}
//for循環(huán)完畢兔魂,檢測(cè)了所有的元素
//0-w之間保存了需要留下的數(shù)據(jù)烤芦,w開(kāi)始以及后面的數(shù)據(jù)全部清空
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;
}
/***************************** Writer and Read Object *************************/
//java.io.Serializable的寫(xiě)入函數(shù)
//將ArrayList的“容量、所有的元素值”都寫(xiě)入到輸出流中
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();
// Write out size as capacity for behavioural compatibility with clone()
//寫(xiě)入“數(shù)組的容量”析校,保持與clone()的兼容性
s.writeInt(size);
//寫(xiě)入“數(shù)組的每一個(gè)元素”
for (int i=0; i<size; i++) {
s.writeObject(elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
//java.io.Serializable的讀取函數(shù):根據(jù)寫(xiě)入方式讀出
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
elementData = EMPTY_ELEMENTDATA;
// Read in size, and any hidden stuff
s.defaultReadObject();
//從輸入流中讀取ArrayList的“容量”
s.readInt(); // ignored
if (size > 0) {
// be like clone(), allocate array based upon size not capacity
ensureCapacityInternal(size);
Object[] a = elementData;
//從輸入流中將“所有元素值”讀出
for (int i=0; i<size; i++) {
a[i] = s.readObject();
}
}
}
關(guān)于ArrayList有以下幾點(diǎn)總結(jié):####
動(dòng)態(tài)擴(kuò)充容量
ArrayList在每次增加元素(可能是1個(gè)构罗,也可能是一組)時(shí)铜涉,都要調(diào)用該方法來(lái)確保足夠的容量。當(dāng)容量不足以容納當(dāng)前的元素個(gè)數(shù)時(shí)遂唧,就設(shè)置新的容量為舊的容量的1.5倍加1芙代,如果設(shè)置后的新容量還不夠,則直接新容量設(shè)置為傳入的參數(shù)(也就是所需的容量)盖彭,而后用Arrays.copyof()方法將元素拷貝到新的數(shù)組纹烹。從中可以看出,當(dāng)容量不夠時(shí)召边,每次增加元素铺呵,都要將原來(lái)的元素拷貝到一個(gè)新的數(shù)組中,非常之耗時(shí)隧熙,也因此建議在事先能確定元素?cái)?shù)量的情況下片挂,才使用ArrayList,否則建議使用LinkedList贞盯。Arrays.copyOf()與System.copyOf()方法
// Arrays.copyOf源碼
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
// 返回?cái)?shù)組組件的類(lèi)型
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
// 使用系統(tǒng)native函數(shù)音念,將數(shù)組復(fù)制到新數(shù)組中
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
- ArrayList遍歷訪(fǎng)問(wèn)的效率
// 基于迭代器遍歷
Integer value = null;
Iterator it = list.iterator();
while (it.hasNext()) {
value = (Integer)it.next();
}
// 基于隨機(jī)訪(fǎng)問(wèn)遍歷
Integer value = null;
int size = list.size();
for (int i = 0; i < size; i++) {
value = (Integer)list.get(i);
}
// 基于增強(qiáng)for循環(huán)遍歷
Integer value = null;
for (Integer integ : list) {
value = integ;
}
三種方式遍歷,經(jīng)過(guò)測(cè)試躏敢,基于隨機(jī)訪(fǎng)問(wèn)(索引號(hào)的方式)闷愤,遍歷速度最快;ArrayList查找速度快件余,而插入和刪除速度較慢肝谭;
- ArrayList支持null類(lèi)型
在查找對(duì)象的索引號(hào),移除指定的對(duì)象時(shí)蛾扇,均進(jìn)行null類(lèi)型進(jìn)行檢查,可見(jiàn)ArrayList支持null類(lèi)型魏滚;