以下是 ArrayList<E>类中定义的一些变量和常量,后面的方法会用到,下面也给予了一些说明。
/**
* Default initial capacity.设置默认容量
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* Shared empty array instance used for empty instances.
*
* 设置空数组,用于手动定义的长度为0的arraylist,例如new ArrayList(0)
*/
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.
*
* 设置空数组,区别于EMPTY_ELEMENTDATA,用于默认定义长度为0的arraylist,
* 例如new ArrayList()
*/
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.
*
* ArrayList的元素存放在数组中。ArrayList的容量就是该数组的长度。如果该属性被定义为
* DEFAULTCAPACITY_EMPTY_ELEMENTDATA的ArrayList,那么当这个ArrayList被添加第一个
* 元素后,其容量会被扩充到DEFAULT_CAPACITY的数值(也就是10).
*/
transient Object[] elementData; // non-private to simplify nested class access
/**
* The size of the ArrayList (the number of elements it contains).
*
* ArrayList的当前大小(包含了多少个元素)
* @serial
*/
private int size;
以下是方法的分析:
/**
* Appends the specified element to the end of this list.
*
* 添加特定元素到list的结尾
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
//因为list的容量要随长度增加而扩充,该方法确保容量在正确范围,后面有方法细节
ensureCapacityInternal(size + 1);
/**
* 因为size是当前长度,所以[size]表示在list的结尾的位置添加元素
* 把索引填上后再++表示长度+1
*/
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
/**
* 判断如果该arraylist是new ArrayList(),则默认容量是10,所以比较+1后的长度和
* 10,二选一取最大的容量作为临时容量储存起来
*/
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
//进一步确保容量正确范围(将上一步的临时容量传进来)
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
/**
* modCount是AbstractList<E>中的属性,ArrayList<E>继承了该抽象类
* modCount是记录该arraylist被修改了多少次
* 因为到这一步基本的判断已经确认了,也就是说到这一定会修改arraylist了,剩下的只是
* 扩容与不扩容的问题了,所以modCount在这里就+1了
*/
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0) //判断是否超过当前数组的长度
//扩容方法
grow(minCapacity);
}
/**
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
*
* 设置数组允许的最大长度
* Integer.MAX_VALUE = 2^31-1
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/**
* 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
*/
private void grow(int minCapacity) {
// 设置元素数组的当前长度为旧的容量
int oldCapacity = elementData.length;
/**
* 扩容一般是增加当前长度的一半
* 因为oldCapacity为当前长度,oldCapacity >> 1表示位运算,达到的效果是:如果
* oldCapacity为偶数则除以2,如果oldCapacity为奇数则先-1再除以2
* 所以newCapacity得到的是原数组长度的150%左右
*/
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(); //如果临时容量<0抛出内存溢出异常
//临时容量和最大数组长度哪个小就返回哪个,也就是容量不能超过最大数组长度
return (minCapacity > MAX_ARRAY_SIZE) ?Integer.MAX_VALUE : MAX_ARRAY_SIZE;
}