学习目标:
先来看看构造方法
无参数构造方法
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
这是无参数的构造方法,是对elementData(元素的数组)进行赋值DEFAULTCAPACITY_EMPTY_ELEMENTDATA(空数组,我们暂时叫做默认空数组),明明是空数组,但是注释确实创建容量是10。好吧继续往下看。
再来看带参数的构造方法
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
//如果initialCapacity>0,创建initialCapacity个元素的数组
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
//等于0时,创建空数组
this.elementData = EMPTY_ELEMENTDATA;
} else {
//否则直接抛出异常
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
再看另一个构造方法
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;
}
}
就是直接将集合转为数组赋值给elementData,同时对size赋值,并且如果size不等于0时,c.toArray might (incorrectly) not return Object[] (see 6260652)。我也是一脸懵逼。等于0时,直接赋值空的数组。
我们接着看add方法,先看一个参数的
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
就是ensureCapacityInternal(size + 1) 并且将e添加到size++的位置,我们来看ensureCapacityInternal(size + 1)方法。
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
看这里
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
还记得空参数的构造方法吗?DEFAULT_CAPACITY = 10。也就是当空参数时创建ArrayList时,minCapacity = 10与minCapacity的最大值,显然创建时minCapacity = 0.这时minCapacity = 10,然后看ensureExplicitCapacity(minCapacity)
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
显然调用无参数构造方法时minCapacity - elementData.length > 0是成立的,我们再看grow(minCapacity)
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
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);
}
oldCapacity是元素个数newCapacity是oldCapacity+oldCapacity/2,即oldCapacity的1.5倍。当1.5倍的元素个数小于minCapacity时newCapacity = minCapacity。显然创建无参数ArrayList就是这种情况,这就解决了开头的第一个问题。调用无参数构造方法创建的是10个元素的长度。
我们再看当1.5倍元素个数大于 MAX_ARRAY_SIZE时newCapacity = hugeCapacity(minCapacity);
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
到这里我们可以总结一下了:
无参构造方法调用add之后创建的是10个长度的数组。有参数则直接创建指定长度的数组。扩容时,小于MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8一次扩容1.5倍,超过则直接Integer.MAX_VALUE。