privatestaticfinallong serialVersionUID =8683452581122892189L;/**
* Default initial capacity.
*/privatestaticfinalint DEFAULT_CAPACITY =10;/**
* Shared empty array instance used for empty instances.
*/privatestaticfinal 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.
*/privatestaticfinal 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).
*
* @serial
*/privateint size;
類型
變量
說明
long
DEFAULT_CAPACITY
初始默認變量,設定爲10
int[]
EMPTY_ELEMENTDATA
給空對象使用的數組
Object[]
DEFAULTCAPACITY_EMPTY_ELEMENTDATA
給指定了大小的對象使用的數組
Object[]
elementData
給其他非空對象使用的數組
細心的人就會發現了,除了最重要的非空數組 elementData 是 default 修飾詞,其他的變量都是 private 修飾的,在 elementData 後面註釋的 non-private to simplify nested class access 是指默認訪問權限可以簡化嵌套類訪問過程,又是什麼意思呢?
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/publicArrayList(int initialCapacity){if(initialCapacity >0){this.elementData =newObject[initialCapacity];}elseif(initialCapacity ==0){this.elementData = EMPTY_ELEMENTDATA;}else{thrownewIllegalArgumentException("Illegal Capacity: "+
initialCapacity);}}/**
* Constructs an empty list with an initial capacity of ten.
*/publicArrayList(){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.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/publicArrayList(Collection<?extendsE> c){
elementData = c.toArray();if((size = elementData.length)!=0){// defend against c.toArray (incorrectly) not returning Object[]// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)if(elementData.getClass()!= Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);}else{// replace with empty array.this.elementData = EMPTY_ELEMENTDATA;}}
構造方法
說明
public ArrayList()
構造一個初始容量爲10的空列表
public ArrayList(int initialCapacity)
構造一個指定初始容量的空列表
public ArrayList(Collection c)
構造一個包含指定集合元素的列表,按迭代器返回的順序
3. API 中的主要方法
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
* @throws OutOfMemoryError if minCapacity is less than zero
*/private Object[]grow(int minCapacity){int oldCapacity = elementData.length;if(oldCapacity >0|| elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA){int newCapacity = ArraysSupport.newLength(oldCapacity,
minCapacity - oldCapacity,/* minimum growth */
oldCapacity >>1/* preferred growth */);return elementData = Arrays.copyOf(elementData, newCapacity);}else{return elementData =newObject[Math.max(DEFAULT_CAPACITY, minCapacity)];}}private Object[]grow(){returngrow(size +1);}/**
* Returns the number of elements in this list.
*
* @return the number of elements in this list
*/publicintsize(){return size;}/**
* This helper method split out from add(E) to keep method
* bytecode size under 35 (the -XX:MaxInlineSize default value),
* which helps when add(E) is called in a C1-compiled loop.
*/privatevoidadd(E e, Object[] elementData,int s){if(s == elementData.length)
elementData =grow();
elementData[s]= e;
size = s +1;}/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/publicbooleanadd(E e){
modCount++;add(e, elementData, size);returntrue;}