一點一點看原始碼(2) ArrayList 未完
阿新 • • 發佈:2018-12-24
ArrayList: * Resizable-array implementation of the <tt>List</tt> interface. Implements * all optional list operations, and permits all elements, including * <tt>null</tt>. In addition to implementing the <tt>List</tt> interface, * this class provides methods to manipulate the size of the array that is * used internally to store the list. (This class is roughly大致的 equivalent 相等於to * <tt>Vector</tt>, except that it is unsynchronized.)
原始碼第一句就給出 了ArrayList是一個實現list集合全部操作的可變陣列,可以有null,陣列大小是可以調整的,執行緒不安全。
每一個都有初始容量,當向集合中新增元素時候會進行擴容。(就不copy了)
它提供了三個構造器:
一個無參構造器ArrayList();
兩個帶參構造器ArrayList(int)和ArrayList(Collection< ? extends E>)
/** * Default initial capacity.預設初始化大小為10 */ private static final int DEFAULT_CAPACITY = 10; /** * Shared empty array instance used for empty instances.以空object陣列作為類內共享例項容器。 */ private static final Object[] EMPTY_ELEMENTDATA = {}; /** * Shared empty array instance used for default sized empty instances. We * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when * first element is added. */ private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; /** * The array buffer into which the elements of the ArrayList are stored. * The capacity of the ArrayList is the length of this array buffer. Any * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA * will be expanded to DEFAULT_CAPACITY when the first element is added. */ transient Object[] elementData; // non-private to simplify nested class access
/**
* The size of the ArrayList (the number of elements it contains).
*/
private int size;
/** * Constructs an empty list with the specified initial capacity. *帶參構造,手動設定容器初始大小,若例項化時能確定大小,最好別使用預設擴充套件容量,將提高執行效率 */ 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); } }
/**
* Constructs an empty list with an initial capacity of ten. 無參構造器 ,初始化容量為10
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.帶參構造器,將引數集合用陣列工具類轉成陣列,並存入緩衝陣列,對緩衝陣列進行校驗.
若轉換後的陣列並非object陣列(內巢狀無法直接引用情況下),則使用陣列工具類將該陣列使用深度拷貝
*/
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;
}
}
/**
* Trims the capacity of this <tt>ArrayList</tt> instance to be the
* list's current size. An application can use this operation to minimize
* the storage of an <tt>ArrayList</tt> instance.去除多餘的陣列申請空間,在記憶體緊張時會用到
*/
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = (size == 0)
? EMPTY_ELEMENTDATA
: Arrays.copyOf(elementData, size);
}
}