ArrayList是实现了List的接口的类,底层数据结构是一个数组,但是该数组实现了可变大小,具备了更好的性能。
那么Arraylist是如何实现可变大小的呢,一起深入源码看一下。
首先看一下它的几个属性
/**
* Default initial capacity.
*/
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;
这是ArrayList类的属性,通过翻译意思我们能够知道DEFAULT_CAPACITY 是数组的默认长度,EMPTY_ELEMENTDATA 是一个空的数组,DEFAULTCAPACITY_EMPTY_ELEMENTDATA 是一个默认的空数组,elementData是数组缓存区,size是Arraylist的元素个数。
ArrayList的三个构造器
/**
* 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);
}
}
指定初始化容量的大小,返回一个指定大小的数组,为0时返回EMPTY_ELEMENTDATA。
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
无参构造,返回默认空数组。默认大小为10。
/**
* 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) {
Object[] a = c.toArray();
if ((size = a.length) != 0) {
if (c.getClass() == ArrayList.class) {
elementData = a;
} else {
elementData = Arrays.copyOf(a, size, Object[].class);
}
} else {
// replace with empty array.
elementData = EMPTY_ELEMENTDATA;
}
}
初始化时传入特定集合的元素,底层将其转换为数字,并将其复制给elementDate;传入集合为空时返回EMPTY_ELEMENTDATA。
扩容
当向ArrayList添加元素时,触发扩容机制。
/**
* Appends the specified element to the end of this 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) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
首先调用ensureCapacityInternal(size + 1)方法,新添加元素时,数组大小加1,确保元素可以被保存。
ensureCapacityInternal()先是调用calculateCapacity(elementData, minCapacity)方法。
如果此时数组是DEFAULTCAPACITY_EMPTY_ELEMENTDATA,这是无参构造返回的数组。若实例化时是无参构造则返回DEFAULT_CAPACITY, minCapacity的最大值,minCapacity此时是1。此时返回默认大小DEFAULT_CAPACITY 值为10。如果不是无参构造返回minCapacity。
然后又调用ensureExplicitCapacity(int minCapacity)
modCount自增,这条语句有什么用至此还不清楚,先忽略继续向下。
如果minCapacity大于elementData的大小,则扩容。
至此扩容才算真正开始,int newCapacity = oldCapacity + (oldCapacity >> 1);是最重要的一条语句,oldCapacity 为原来的容量,oldCapacity >> 1 表示向右移一位,相当于除以2,即newCapacity 是原来容量的1.5倍。如果扩大1.5倍后还不够,则直接将其扩大至minCapacity大小。判断newCapacity是否 超过了最大容量MAX_ARRAY_SIZE ,
若超过则执行hugeCapacity(minCapacity)
判断minCapacity是否大于MAX_ARRAY_SIZE,如果大于则直接扩大值Integer最大值,否则返回MAX_ARRAY_SIZE。
最后通过Arrays.copyOf(elementData, newCapacity);将原来数据复制到扩容后的数组中。elementData[size++] = e;将新加入元素添加至数组中。