ArrayList-源码分析-自动扩容机制
ArrayList类:
public class ArrayList....{ ...... private static final int DEFAULT_CAPACITY = 10; //默认容量 10 transient Object[] elementData; //存放元素的数组 private int size; //实际元素个数 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; //没有元素时的默认容量 也就是 0 ...... }
无参构造
public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; }
通过构造方法可以知道,新创建的 ArrayList 容量是0,size=0
以add方法为入口分析 ArrayList 的复制宽容机制:
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
add方法成功时返回true,过程调用ensureCapacityInternal,新建时size等于0
private void ensureCapacityInternal(int minCapacity) { ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); }
ensureExplicitCapacity 调用 ensureExplicitCapacity,传参 calculateCapacity(elementData, minCapacity)
private static int calculateCapacity(Object[] elementData, int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { return Math.max(DEFAULT_CAPACITY, minCapacity); } return minCapacity; }
新建时,我们已经看过无参构造中把 DEFAULTCAPACITY_EMPTY_ELEMENTDATA 赋值给 elementData ,所以上面的方法中if条件成立,返回 Math.max(DEFAULT_CAPACITY, minCapacity);
而 DEFAULT_CAPACITY 是10,所以当新建或装入的元素个数小于10时,都是返回10,当大于10的时候,返回的时minCapacity,而 minCapacity 其实等于 size + 1。
执行完 calculateCapacity,将结果传入 ensureExplicitCapacity:
private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); }
如果元素个数小于10,传入的数就是10,此时 minCapacity - elementData.length > 0 成立,执行 grow(10)。
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); }
grow( int minCapacity ) 就是其扩容方法,元素个数小于10时 ,例如 新建时 oldCapacity = 0;newCapacity = 0;第一个if成立,newCapacity = 10 ,进入第二个if ,这里 MAX_ARRAY_SIZE 的定义:
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
也就是说 MAX_ARRAY_SIZE非常大,对于我们正常开发中的实际情况,第二个if都是不成立的,如果成立将会调用 hugeCapacity方法,这种情况很少,我们继续往下:
elementData = Arrays.copyOf(elementData, newCapacity);
执行Arrays.copyOf 后 elementData 容量从0到了10。
10个以后呢?
带着size=10,从新走一遍上面的代码:
进入add方法后执行:ensureCapacityInternal(10+ 1):
private void ensureCapacityInternal(int minCapacity) { ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); }
先调用
private static int calculateCapacity(Object[] elementData, int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { return Math.max(DEFAULT_CAPACITY, minCapacity); } return minCapacity; }
返回 minCapacity,进入 ensureExplicitCapacity(11):
private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); }
条件11-10>0成立进入 grow(11):
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 = 10,newCapacity = 10 + 10/2 = 15;第一个if不成立,第二个条件不成立,执行 Arrays.copyOf方法,elementData 完成了复制元素并把容量从10变成15。
总结:
没有向集合中添加任何元素时,容量0,添加一个元素后容量10,当超过10个后,每次以1.5倍的扩充容量。