标签:线程安全 arraylist size int 复制 col set 存储 object
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
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)
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方法
默认扩容为之前的1.5倍,如果容量不够则直接扩充为加入当前元素后的大小
然后利用Arrays.copyOf进行数组的复制
public static native void arraycopy(
Object src,
int srcPos,
Object dest,
int destPos,
int length
);
标签:线程安全 arraylist size int 复制 col set 存储 object
原文地址:http://www.cnblogs.com/zj90/p/7648548.html