对于ArraysList的添加,首先会对当前容量进行判断
//添加操作
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e; //添加元素并把数组长度+1
return true;
}
//确保内部容量,容量不足就执行扩容操作
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
创建ArraysList对象时,初始容量为0,当执行添加操作,就会判断到elementData为空,此时就创建一个容量为10的新数组,覆盖原来的elementData数组,初始化成功
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { //数组为空返回 DEFAULT_CAPACITY(10)
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity; //当数组有数据时就直接返回+1后的长度
}
对当前容量进行判断
- 当添加时的size++不超过elementData数组的容量length,就不扩容
- 当添加时的size++超过elementData数组的容量length,就会执行扩容
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code, length为数组当前容量
if (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) //初始化数组容量10
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0) //扩容后数组容量 > MAX_ARRAY_SIZE(2的31次幂-8)时
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
扩容会有一个最大容量限制
//限制最大扩容量为 Integer.MAX_VALUE
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
根据扩容后容量值,把原数组数据复制到新创建的数组,扩容成功
//执行Arrays数组的copyOf方法【】10
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
//
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength] //空数组时new一个容量为10的新数组
: (T[]) Array.newInstance(newType.getComponentType(), newLength); //new一个容量为扩容后的数组
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength)); //把原数组放到新数组前面,保证新添加数据在原数组后面
return copy;
}
//Array.newInstance方法
class Array {
public static Object newInstance(Class<?> componentType, int length)
throws NegativeArraySizeException {
return newArray(componentType, length);
}
private static native Object newArray(Class<?> componentType, int length)
throws NegativeArraySizeException;
}