首先简单的总结一下ArrayList扩容机制:
-
- 使用无参构造器,初始容量为0或1,第一次添加扩容为10,再次扩容为1.5倍(向下取整)
- 使用指定大小的构造器,初始容量为指定大小,再次扩容为1.5倍(向下取整)
为什么初始容量为0时第一次扩容为10?
为什么扩容是1.5倍?
下面通过一个例子,结合源码分析扩容原理。
创建一个arraylist对象,添加一个元素
ArrayList arrayList = new ArrayList();
arrayList.add("obj1");
下面打开JDK11中ArrayList源码,分析一下这个过程中是如何扩容的
①调用无参构造器,此时容量为0
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
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
DEFAULTCAPACITY_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 = {};
②添加元素,调用add(E e)方法
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
modCount++;
add(e, elementData, size);
return true;
}
该方法调用一个重载的add(E e, Object[] elementData, int s)方法
/**
* This helper method split out from add(E) to keep method
* bytecode size under 35 (the -XX:MaxInlineSize default value),
* which helps when add(E) is called in a C1-compiled loop.
*/
private void add(E e, Object[] elementData, int s) {
if (s == elementData.length)
elementData = grow();
elementData[s] = e;
size = s + 1;
}
此时size和elementData.length都为0
③调用grow()方法
private Object[] grow() {
return grow(size + 1);
}
该方法调用重载的grow(int minCapacity)方法,size+1为传入的minCapacity参数
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
* @throws OutOfMemoryError if minCapacity is less than zero
*/
private Object[] grow(int minCapacity) {
return elementData = Arrays.copyOf(elementData,
newCapacity(minCapacity));
}
④调用newCapacity(int minCapacity)进行扩容
/**
* Returns a capacity at least as large as the given minimum capacity.
* Returns the current capacity increased by 50% if that suffices.
* Will not return a capacity greater than MAX_ARRAY_SIZE unless
* the given minimum capacity is greater than MAX_ARRAY_SIZE.
*
* @param minCapacity the desired minimum capacity
* @throws OutOfMemoryError if minCapacity is less than zero
*/
private int newCapacity(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity <= 0) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
return Math.max(DEFAULT_CAPACITY, minCapacity);
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return minCapacity;
}
return (newCapacity - MAX_ARRAY_SIZE <= 0)
? newCapacity
: hugeCapacity(minCapacity);
}
因为当前oldCapacity为0,进行扩容计算后的newCapacity还是0,进入if语句,此时elementData还是默认空数组,返回DEFAULT_CAPACITY, minCapacity二者中最大的一个,minCapacity为size+1,也就是1,而DEFAULT_CAPACITY定义如下:
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
10>1,所以新的容量为10
再之后如果还需要扩容,则通过 新容量 = 旧容量 + 旧容量右移一位 进行计算
例如 新容量 = 10 + (10>>1) = 1010 + 0101 = 10 + 5 = 15
int newCapacity = oldCapacity + (oldCapacity >> 1);
所以,准确的计算扩容容量需要使用上面的公式,1.5倍并不是很严谨