1.ArrayList源码
ArrayList 有一个初始化的容量是10 MIN_CAPACITY_INCREMENT =10
add方法源码
object[] a = array;
int s = size;
if (s == a.length){
object[] newArray = new Object[ s+
(s < MIN_CAPACITY_INCREMENT / 2) ? MIN_CAPACITY_INCREMENT :s >> 1)];
}
System.arraycopy(a,0,newArray,0,s);
a[s] = object;
size = newSize;
解释:当长度等于数组长度的时候 开始扩容
网友评论