一讯柔、ArrayList簡介
ArrayList是可以動態(tài)增長和縮減的索引序列抡蛙,它是基于數(shù)組實現(xiàn)的List類。
該類封裝了一個動態(tài)再分配的Object[]數(shù)組魂迄,每一個類對象都有一個capacity屬性粗截,表示它們所封裝的Object[]數(shù)組的長度,當(dāng)向ArrayList中添加元素時极祸,該屬性值會自動增加。如果想ArrayList中添加大量元素怠晴,可使用ensureCapacity方法一次性增加capacity遥金,可以減少增加重分配的次數(shù)提高性能。
ArrayList的用法和Vector向類似蒜田,但是Vector是一個較老的集合稿械,具有很多缺點,不建議使用冲粤。另外美莫,ArrayList和Vector的區(qū)別是:ArrayList是線程不安全的,當(dāng)多條線程訪問同一個ArrayList集合時梯捕,程序需要手動保證該集合的同步性厢呵,而Vector則是線程安全的。
二:源碼分析
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
/**
* Default initial capacity.默認初始容量
*/
private static final int DEFAULT_CAPACITY = 10;
/ **
* 空數(shù)組
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
/**
* 用于默認大小空實例的共享空數(shù)組實例傀顾。我們
* 將其與empty_elementdata區(qū)分開來襟铭,以了解在什么時候
* 第一個元素被添加。
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
存儲ArrayList元素的數(shù)組緩沖區(qū)。
ArrayList的容量是這個數(shù)組緩沖區(qū)的長度寒砖。
一個空的ArrayList當(dāng) elementData==DEFAULTCAPACITY_EMPTY_ELEMENTDATA第一個元素被添加時赐劣,容量將擴展為DEFAULT_CAPACITY。
*/
transient Object[] elementData;
/**
* The size of the ArrayList (the number of elements it contains).
*ArrayList的大辛ǘ肌(包含的元素個數(shù))
* @serial
*/
private int size;
/**
* 用指定的初始容量構(gòu)造一個空列表魁兼。
*
* @param initialCapacity 初始容量
* @throws IllegalArgumentException 如果指定的初始容量是負的將拋出異常
*/
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
/**
* 構(gòu)造一個空列表,初始容量為10漠嵌。
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
/**
構(gòu)造一個包含指定元素的列表的列表集合咐汞,按照集合的順序返回迭代器。
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
/**
將這個ArrayList實例的容量變成列表的當(dāng)前大小献雅。
應(yīng)用程序可以使用這個操作來最小化存儲一個ArrayList的實例碉考。
**/
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = (size == 0)
? EMPTY_ELEMENTDATA
: Arrays.copyOf(elementData, size);
}
}
/**
增加這個ArrayList實例的容量,確保它至少能容納元素的數(shù)量
由最小容量參數(shù)指定挺身。
@param minCapacity 期望的最小容量
*/
public void ensureCapacity(int minCapacity) {
int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
// 任何大小侯谁,如果不是默認元素表則為零
? 0
//如果是默認的數(shù)組則為 DEFAULT_CAPACITY
: DEFAULT_CAPACITY;
//如果期望的最小容量比 minExpand大 則擴容
if (minCapacity > minExpand) {
ensureExplicitCapacity(minCapacity);
}
}
/**
要分配的數(shù)組的最大大小。
數(shù)組作為一個對象章钾,需要一定的內(nèi)存存儲對象頭信息墙贱,對象頭信息最大占用內(nèi)存不可超過8字節(jié),所以這里最大長度為 -8
試圖分配更大的數(shù)組可能導(dǎo)致
OutOfMemoryError:請求的數(shù)組大小超過VM限制
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/**
增加容量贱傀,以確保它至少可以容納
由最小容量參數(shù)指定的元素數(shù)量惨撇。
*
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
//新容量=old+old/2 ,即原來的1.5倍
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
//當(dāng)容量超過MAX_ARRAY_SIZE,最大容量允許大于 MAX_ARRAY_SIZE
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;
}
/**
* 如果該列表包含指定的元素返回true
* @param o element whose presence in this list is to be tested
* @return <tt>true</tt> if this list contains the specified element
*/
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
/**
*添加一個元素府寒,ensureCapacityInternal(minCapacity)判斷最小容量和數(shù)組實際大小魁衙,如果最小容量比數(shù)組大則進行擴容為原來的1.5倍
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
/**
移除下標的為index的元素
*
* @param index the index of the element to be removed
* @return the element that was removed from the list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
//注意這里,將數(shù)組index以后的都向前移動一位
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
//將數(shù)組最后一位置為null
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}
/**
移除指定元素:如果傳入的元素為空株搔,則刪除第一個出現(xiàn)的空元素剖淀,如果沒有空元素則返回 false
如果傳入的不為空,則刪除第一個出現(xiàn)的元素纤房,不存在返回false
*
* @param o element to be removed from this list, if present
* @return <tt>true</tt> if this list contained the specified element
*/
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
}
/**
* 清空一個list
* be empty after this call returns.
*/
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
}