addAll方法和申请数组大小函数
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) { //判断是否大于默认申请空间
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1; //增加50%+1
if (newCapacity < minCapacity) //如果还是不够,直接让其等于最新的长度
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity); //新的数组
}
}
照例来看一下addAll方法,ensureCapacityInternal判断当前的数字是否大于了申请的长度,如果空间不够了,在申请 1.5倍的空间+1,如果还是不够,直接让最新的长度等于需要的长度,然后将旧的数组给新的数组。
参考:http://blog.csdn.net/ZXC641483573/article/details/78106552?locationNum=7&fps=1