package java.util;
* @author Josh Bloch
* @author Neal Gafter
* @see Collection
* @see List
* @see LinkedList
* @see Vector
* @since 1.2
*/
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L;
* 默认初始化容量
*/
private static final int DEFAULT_CAPACITY = 10;
* 用来共享的空数组实例
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
* The array buffer into which the elements of the ArrayList are stored.
* ArrayList存储在该数组中,容量就是数组的大小。任何空的ArrayList的elementData和EMPTY_ELEMENTDATA相等。
* 当添加第一个元素的时候容量扩展为DEFAULT_CAPACITY
*/
private transient Object[] elementData;
* ArrayList的size,这个数字是ArrayList的内容个数
* @serial
*/
private int size;
* 该构造方法创建一个空的list初始化容量是由参数指定的
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
* Constructs an empty list with an initial capacity of ten. 这是1.6版本的注释,
* 现在这个版本默认创建后是0长度的数组,第一次添加时才初始化容量
* 空的构造方法,默认是空的数组
*/
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
* 这是1.6版本的空构造方法
*
*/
public ArrayList() {
this(10);
}
*构造一个包含指定集合的元素的列表,按照它们由集合的迭代器返回的顺序。
* @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();
size = elementData.length;
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
* 将集合的数组长度变成和集合size
*/
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = Arrays.copyOf(elementData, size);
}
}
* 添加集合的容量,确保增加指定参数的最小容量。其实也就是先往小的加
*/
public void ensureCapacity(int minCapacity) {
int minExpand = (elementData != EMPTY_ELEMENTDATA)
? 0
: DEFAULT_CAPACITY;
if (minCapacity > minExpand) {
ensureExplicitCapacity(minCapacity);
}
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
* 数组最大能分配的值
* 一些虚拟机保留了一些头信息
* 尝试分配一些大的数组可能的结果是OutOfMemoryError,请求数组的size超过了VM限制
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
* 添加集合的容量,确保增加指定参数的最小容量。其实也就是先往小的加
*/
private void grow(int minCapacity) {
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0)
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
* 返回该list的元素个数
* @return the number of elements in this list
*/
public int size() {
return size;
}
* 如果该list不包含任何元素则返回true
* @return <tt>true</tt> if this list contains no elements
*/
public boolean isEmpty() {
return size == 0;
}
* 如果该list包含指定的元素则返回true
*/
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
* 返回的第一个匹配指定元素的索引,如果返回-1表示这个list集合没有包含该元素。
* 更加准确的说是返回匹配的最小的索引,从0开始到size进行循环
*/
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
* 反向查询元素,如果返回-1表示这个list集合没有包含该元素。
* 更加准确的说是返回匹配的最大的索引,从size开始到0进行循环
*/
public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size-1; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = size-1; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
* 复制集合
* @return a clone of this <tt>ArrayList</tt> instance
*/
public Object clone() {
try {
@SuppressWarnings("unchecked")
ArrayList<E> v = (ArrayList<E>) super.clone();
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
* 将集合转换成数组,该数组包含集合的所有元素。该数组的元素顺序和list集合一致
* 该方法会新创建一个数组实例
*/
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
* 将集合转换成数组返回
*/
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if (a.length < size)
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
@SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}
* 返回指定索引元素
*/
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
* 替换指定索引元素,并返回被替换的元素
*/
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
* 追加指定的元素到集合的末尾,第一次添加的时候会指定数组的默认length。
* 这个和1.6版本不同
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1);
elementData[size++] = e;
return true;
}
* 在集合的指定位置添加元素
*/
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1);
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
* 移除一个元素,如果这个元素不是最后还涉及元素移动的操作
*/
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null;
return oldValue;
}
* 这个和索引remove一样,只是多了一步查找元素索引的操作。
*/
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;
}
* 参考remove(int index)方法
*/
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;
}
* 清除集合中的所有元素
*/
public void clear() {
modCount++;
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
* 添加指定的集合到该ArrayList的的最后,如果有其他线程修改了入参,
* 将不会影响该方法的操作
*/
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew);
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
* 参考addAll(Collection<? extends E> c)方法,该方法增加了元素的移动
*/
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew);
int numMoved = size - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
* 移除给定范围内的数据
*/
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
int newSize = size - (toIndex-fromIndex);
for (int i = newSize; i < size; i++) {
elementData[i] = null;
}
size = newSize;
}
* 越界校验
*/
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
* 校验合法的索引
*/
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
* 异常信息组装
*/
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
* 移除给定集合包含的元素
*/
public boolean removeAll(Collection<?> c) {
return batchRemove(c, false);
}
* 保留给定集合的元素
*/
public boolean retainAll(Collection<?> c) {
return batchRemove(c, true);
}
private boolean batchRemove(Collection<?> c, boolean complement) {
final Object[] elementData = this.elementData;
int r = 0, w = 0;
boolean modified = false;
try {
for (; r < size; r++)
if (c.contains(elementData[r]) == complement)
elementData[w++] = elementData[r];
} finally {
if (r != size) {
System.arraycopy(elementData, r,
elementData, w,
size - r);
w += size - r;
}
if (w != size) {
for (int i = w; i < size; i++)
elementData[i] = null;
modCount += size - w;
size = w;
modified = true;
}
}
return modified;
}
}