ArrayList源码解析
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>
实现接口
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
基本属性
transient Object[] elementData; //存放数组的元素,transient表示该字段不进行序列化操作
private int size; //已经放入数组中的元素个数,非数组的长度
源码解析
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
//可序列化版本号
private static final long serialVersionUID = 8683452581122892189L;
//默认的初始化数组大小 为10 .
private static final int DEFAULT_CAPACITY = 10;
//实例化一个空数组
private static final Object[] EMPTY_ELEMENTDATA = {};
//存放List元素的数组
private transient Object[] elementData;
//List中元素的数量,和存放List元素的数组长度可能相等,也可能不相等
private int size;
//构造方法,指定初始化的数组长度
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
//无参构造方法
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
//构造方法,参数为集合元素
public ArrayList(Collection<? extends E> c) {
//将集合转换成数组,并赋值给elementData数组
elementData = c.toArray();
size = elementData.length;
//如果c.toArray返回的不是Object[]类型的数组,转换成Object[]类型
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
//改变数组的长度,使长度和List的size相等。
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = Arrays.copyOf(elementData, size);
}
}
//确定ArrayList的容量
//判断当前elementData是否是EMPTY_ELEMENTDATA,若是设置长度为10
public void ensureCapacity(int minCapacity) {
int minExpand = (elementData != EMPTY_ELEMENTDATA)
// any size if real element table
? 0
// larger than default for empty table. It's already supposed to be
// at default size.
: 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++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
//数组的最大容量
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
//扩容操作
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
//容量扩充1.5倍
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:
//生成一个长度为newCapacity数组,并将elementData数组中元素拷贝到新数组中,并将新数组的引用赋值给elementData
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
//返回数组中已经放入的元素个数,非数组长度
public int size() {
return size;
}
//List是否为空
public boolean isEmpty() {
return size == 0;
}
//判断是否包包含指定元素
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
//查找指定的元素,存在返回下标,不存在放回 -1
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
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;
}
//因为实现了clone接口,所以需要重写clone()方法,实现对象的拷贝
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) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}
//将集合转化为数组
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
//转化为指定类型的数组元素,推荐使用此方法
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);