ArrayList簡介
ArrayList實現(xiàn)了List接口定罢,內(nèi)部以數(shù)組存儲數(shù)據(jù)吼鱼,允許重復的值韩脑。由于內(nèi)部是數(shù)組實現(xiàn)劫谅,所以ArrayList具有數(shù)組所有的特性,通過索引支持隨機訪問嚷掠,查詢速度快捏检,但是插入和刪除的效率比較低。
ArrayList默認初始容量為10不皆,每次添加新元素時都會檢查是否需要擴容操作贯城。擴容操作需要重新拷貝數(shù)組,比較耗時霹娄,所以如果預先能知道數(shù)組的大小能犯,在初始化時候可以指定一個初始容量。
ArrayList不是線程安全的犬耻,使用時應注意踩晶。
源碼分析
jdk1.7.0_71
//默認容量
private static final int DEFAULT_CAPACITY = 10;
//默認空的數(shù)組,定義空的ArrayList
private static final Object[] EMPTY_ELEMENTDATA = {};
//保存ArrayList中元素的數(shù)組,以下基本操作都是基于該數(shù)組
private transient Object[] elementData;
//ArrayList中元素數(shù)組的容量
private int size;
無參構(gòu)造函數(shù)
//構(gòu)造一個空的Object數(shù)組枕磁,初始容量為10
public ArrayList() {}
指定容量大小的構(gòu)造函數(shù)
//指定初始容量
public ArrayList(int initialCapacity) {}
指定初始值為繼承Collection接口的集合的構(gòu)造函數(shù)
public ArrayList(Collection<? extends E> c) {}
trimToSize
//ArrayList每次增長會預申請1.5倍+1的空間,
//舉個例子就是當size() = 1000的時候渡蜻,ArrayList已經(jīng)申請了1200空間
//trimToSize 的作用是刪除多余的200
public void trimToSize() {
modCount++;
...
}
關(guān)于modCount
modCount定義在抽象類AbstratList中, 源碼的注釋基本說明了它的用處:在使用迭代器遍歷的時候,用來檢查列表中的元素是否發(fā)生結(jié)構(gòu)性變化(列表元素數(shù)量發(fā)生改變的一個計數(shù))了茸苇,主要在多線程環(huán)境下需要使用排苍,防止一個線程正在迭代遍歷,另一個線程修改了這個列表的結(jié)構(gòu)学密。
ensureCapacity
//增加此ArrayList實例的容量淘衙,如果需要,以確保其能容納至少由最小容量參數(shù)指定的元素數(shù)
public void ensureCapacity(int minCapacity) {}
grow()方法,實際的擴容方法,擴充為原來的1.5倍
//數(shù)組最大值
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);
}
size() ArrayList的大小
public int size() {}
isEmpty() 不包含元素,返回true
public boolean isEmpty() {}
contains()包含指定的元素,返回true
public boolean contains(Object o) {}
indexOf()
public int indexOf(Object o) {}
lastIndexOf()
public int lastIndexOf(Object o) {}
clone() 淺拷貝
public Object clone() {}
toArray() 返回Object數(shù)組
public Object[] toArray() {}
toArray(T[] a)
//返回數(shù)組的運行時類型是指定數(shù)組的腻暮。如果列表中指定的數(shù)組能容納彤守,則在其中返回。
//否則西壮,一個新的數(shù)組分配具有指定數(shù)組的運行時類型和此列表的大小遗增。
//如果列表中指定的數(shù)組能容納更加節(jié)省空間(即數(shù)組的元素比列表元素多),
//那么會將緊挨著collection尾部的元素設(shè)置為null款青。
public <T> T[] toArray(T[] a) {}
elementData() 根據(jù)索引返回數(shù)據(jù)
E elementData(int index) {}
get(int index) 根據(jù)索引獲取
public E get(int index) {
rangeCheck(index);//范圍檢查,看是否給定的index是否超出數(shù)組大小
...
}
set(int index, E element) 根據(jù)索引替換
public E set(int index, E element) {
rangeCheck(index);
...
}
add(E e) 添加元素到元素末尾
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
...
}
add(int index,E element) 添加元素到指定位置
public void add(int index, E element) {
rangeCheckForAdd(index);
...
}
remove(int index)刪除指定位置的元素
public E remove(int index) {
...
elementData[--size] = null; // clear to let GC do its work
...
}
remove(Object o) 刪除指定元素
public boolean remove(Object o) {
...
fastRemove(index);
...
}
fastRemove(int index)快速刪除,不檢查邊界,無返回值
private void fastRemove(int index) {}
clear() 清空
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
addAll(Collection<? extends E> c) 添加指定的集合到末尾
public boolean addAll(Collection<? extends E> c) {}
addAll(int index, Collection<? extends E> c) 添加指定元素到指定的位置
public boolean addAll(int index, Collection<? extends E> c) {}
removeRange(int fromIndex, int toIndex) 刪除指定區(qū)間的元素
protected void removeRange(int fromIndex, int toIndex) {}
removeAll(Collection<?> c) 刪除指定的集合元素
public boolean removeAll(Collection<?> c) {
return batchRemove(c, false);
}
retainAll(Collection<?> c) 保留指定集合元素
public boolean retainAll(Collection<?> c) {
return batchRemove(c, true);
}
listIterator(int index)和 listIterator() 返回一個迭代器,支持向前和向后遍歷
public ListIterator<E> listIterator(int index) {}
public ListIterator<E> listIterator() {}
iterator() 只能向后遍歷
public Iterator<E> iterator() {}
subList() 返回部分list
public List<E> subList(int fromIndex, int toIndex) {}
http://blog.csdn.net/crave_shy/article/details/17436773
http://www.importnew.com/19233.html