一、构造方法

1.创建一个空ArrayList

public ArrayList() {
    this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}

DEFAULTCAPACITY_EMPTY_ELEMENTDATA为private static final的Object[],值为{}

2.创建ArrayList对象的时候,把一个Collection或其子类添加进来

public ArrayList(int initialCapacity) {
    if (initialCapacity > 0) {
        this.elementData = new Object[initialCapacity];
    } else if (initialCapacity == 0) {
        this.elementData = EMPTY_ELEMENTDATA;
    } else {
        throw new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
    }
}

initialCapacity为初始化容量
EMPTY_ELEMENTDATA也为private static final的Object[],值为{}

3.创建ArrayList对象的时候,把一个Collection或其子类添加进来

public ArrayList(Collection<? extends E> c) {
    elementData = c.toArray();
    if ((size = elementData.length) != 0) {
        // c.toArray()可能没有正确的返回一个Object数组对象
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, size, Object[].class);
    } else {
        // 创建一个空的ArrayList
        this.elementData = EMPTY_ELEMENTDATA;
    }
}

二、添加

添加成功返回boolean类型值

public boolean add(E e) {
    //字面意思:确保容量存在,计算最小初始容量。
    ensureCapacityInternal(size + 1);  
    elementData[size++] = e;
    return true;
}

如果这个ArrayList是刚刚初始化好的(容量为0),则为默认值:10;若容量不固定,则比较size与10的大小,返回最大值。

private void ensureCapacityInternal(int minCapacity) {
    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
    }

    ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
    modCount++;
    // 现在最小容量大小超过现在数组的长度,增长
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}
private void grow(int minCapacity) {
    int oldCapacity = elementData.length;
    int newCapacity = oldCapacity + (oldCapacity >> 1);//自增1.5倍
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;
    if (newCapacity - MAX_ARRAY_SIZE > 0)//大于MAX_ARRAY_SIZE
        newCapacity = hugeCapacity(minCapacity);
    elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
    if (minCapacity < 0) // 溢出
        throw new OutOfMemoryError();
    return (minCapacity > MAX_ARRAY_SIZE) ? //三元表达式,大于MAX_ARRAY_SIZE取整形最大值
        Integer.MAX_VALUE :
        MAX_ARRAY_SIZE;
}

MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

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) {
    // 用新的长度创建一个新数组
    T[] copy = ((Object)newType == (Object)Object[].class)
        ? (T[]) new Object[newLength]
        : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    // 把原数组的内容拷贝到新数组中,并返回新数组
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;
}

发给官兵
47 声望23 粉丝

态度决定行为,行为决定习惯,习惯决定性格,性格决定命运