HengYk Blog

个人站

具有浪漫主义情调的理想主义务实青年


JDK1.7中的ArrayList

参考博文

ArrayList 的实现原理

构造方法

支持随机访问,支持增删改查操作、克隆、序列化。

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable,         
        java.io.Serializable
public ArrayList(int initialCapacity) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
        // 开辟数组空间
        this.elementData = new Object[initialCapacity];
    }
Add系列方法

public boolean add(E e)

public boolean add(E e) {
        // 确保开辟了足够的数组空间
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        // 添加元素
        elementData[size++] = e;
        return true;
    }
private void ensureCapacityInternal(int minCapacity) {

        // 确保数组不为空
        if (elementData == EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }

        // 确保数组容量够用
        ensureExplicitCapacity(minCapacity);
    }
private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

        // overflow-conscious code
        // 需求容量minCapacity比数据的实际容量大,则扩容
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        // 新数组扩容为原数组大小的1.5倍
        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);
    }

其他方法

// 指定位置添加新元素
public void add(int index, E element) {
        // index正确性检测
        rangeCheckForAdd(index);
        // 确保足够的容量
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1, size - index);
        elementData[index] = element;
        size++;
    }
public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
        return numNew != 0;
    }
public boolean addAll(int index, Collection<? extends E> c) {
        rangeCheckForAdd(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount

        int numMoved = size - index;
        if (numMoved > 0)
            System.arraycopy(elementData, index, elementData, index + numNew, numMoved);

        System.arraycopy(a, 0, elementData, index, numNew);
        size += numNew;
        return numNew != 0;
    }
Get方法
public E get(int index) {
        rangeCheck(index);

        return elementData(index);
    }
Set方法
public E set(int index, E element) {
        rangeCheck(index);

        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }
Remove方法
public E remove(int index) {
        rangeCheck(index);

        modCount++;
        E oldValue = elementData(index);

        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

        return oldValue;
    }
Fail-fast机制
final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }