目录
成员变量及构造方法
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L;
/**
* Default initial capacity. 数组初始长度=10
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* Shared empty array instance used for empty instances. 空的数组对象
*/
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).
* 列表元素的个数
* @serial
*/
private int size;
/**
* 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
*/
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.
*/
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.
*
* @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;
}
}
1. 调用无参构造时不会对数组elementData进行初始化,只将它指向一个空的数组对象,在第一次添加元素时初始化
2. 有初始能力(initialCapacity)参数的构造则直接初始化数组elementData
3. 第三个构造public ArrayList(Collection<? extends E> c)将另一个集合中的元素拷贝到elemntData
Add方法
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
ensureCapacityInternal()方法有另个目的:1. 将列表元素的操作数modCount+1(为了保证迭代器在迭代过程中,列表没有被其他线程修改) 2. 判断是否需要扩容
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
//判断capacity是设为默认初始值还是minCapacity
private static int calculateCapacity(Object[] elementData, int minCapacity) {
//如果是通过无参构造new的list对象,且尚未添加过元素,容量设为默认初始值10
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
//否则当前所需最小容量为minCapacity
return minCapacity;
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++; // list更改次数+1
// overflow-conscious code
if (minCapacity - elementData.length > 0) //如果所需最小容量大于当前数组长度,扩容
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
//新容量是旧容量的1.5倍
int newCapacity = oldCapacity + (oldCapacity >> 1);
//如果1.5倍不够,新容量设为所需最小容量
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
//如果新容量比最大容量大,容量设为Integer.MAX_VALUE
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
//将旧的数组拷贝的长度为newCapacity的新数组
elementData = Arrays.copyOf(elementData, newCapacity);
}
Add(int index)方法
public void add(int index, E element) {
//判断index是否属于 0 ~ size-1
rangeCheckForAdd(index);
//modcount+1 可能扩容
ensureCapacityInternal(size + 1); // Increments modCount!!
//将index位置开始的后面的size - index长度的元素复制到index+1开始的位置(相当于后移一位)
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
//index位置放element
elementData[index] = element;
size++;
}
addAll(Collection<? extends E> c)方法
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount
//将新元素加在末尾
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
remove(int index)方法
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
//将index+1开始长度为numMoved的元素往前移一位
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
//多余的一位设为null以便垃圾回收
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}