ArrayList源码阅读
public boolean add(E e) {
//先扩容,size+1作为最小需要的扩容的容量
ensureCapacityInternal(size + 1); // Increments modCount!!
//再放入
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
private static int calculateCapacity(Object[] elementData, int minCapacity) {
//判断是不是默认的空数组
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
//DEFAULT_CAPACITY = 10;
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
//返回最小需要扩容的容量
return minCapacity;
}
private void ensureExplicitCapacity(int minCapacity) {
//记录修改的次数
modCount++;
//如果最小扩容的容量大于当前的容量,说明需要扩容
if (minCapacity - elementData.length > 0)
//扩容
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
//新的容量=原来容量+原来容量的1/2
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
//扩容,并且拷贝到新的数组
elementData = Arrays.copyOf(elementData, newCapacity);
}
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")
//先按照newLength大小创建数组
T[] copy = ((Object)newType == (Object)Object[].class)
//对象数组类型的List类型的
? (T[]) new Object[newLength]
//基本数组类型的
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
//把original赋值到copy中
System.arraycopy(original, 0, copy, 0,Math.min(original.length, newLength));
return copy;
}
public boolean remove(Object o) {
//判断是否为空
if (o == null) {
//为空删掉第一个为空的数据
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
private void fastRemove(int index) {
//记录修改次数
modCount++;
//开始的位置,因为下标是从0开始,要多减一个1
int numMoved = size - index - 1;
if (numMoved > 0)
//相当于在原数组的基础上进行移动?
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}