ArrayList核心代码阅读

public static class ArrayList {

    // 默认的初始容量
    private static final int DEFAULT_CAPACITY = 10;

    // 底层数据结构
    private static final Object[] EMPTY_ELEMENTDATA = {};

    // 扩容时使用的数组
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

    // ArrayList数组的最大容量
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

    // 暂时存储数组元素,作为一个缓冲区
    transient Object[] elementData; // non-private to simplify nested class access

    // 数组中的元素数量
    private int size;

    // 初始化数组指定容量
    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);
        }
    }

    // 空参构造函数,初始化为一个空数组
    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

    // 如果当前数组为空,则返回默认容量10作为数组的容量,否则返回给定的最小容量
    private static int calculateCapacity(Object[] elementData, int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            return Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        return minCapacity;
    }

    // 计算超过最大容量时的容量
    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
                Integer.MAX_VALUE :
                MAX_ARRAY_SIZE;
    }

    // 将元素e添加至数组末尾,ensureCapacityInternal确保数组容量能够容纳要添加的元素数量
    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

    // 检查数组容量是否满足要求
    private void ensureCapacityInternal(int minCapacity) {
        ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
    }

    // 判断容量是否充足,不足则按grow()方法扩容
    private void ensureExplicitCapacity(int minCapacity) {
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

    // 将数组容量按照原来的1.5倍扩容,如果新容量小于最小要求容量,则使用最小要求容量
    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);
    }

    // 查找数组元素第一次出现的索引,如果没有找到返回 -1
    public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i] == null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

    // 查找数组元素最后一次出现的索引,如果没有找到返回 -1
    public int lastIndexOf(Object o) {
        if (o == null) {
            for (int i = size - 1; i >= 0; i--)
                if (elementData[i] == null)
                    return i;
        } else {
            for (int i = size - 1; i >= 0; i--)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }
    
    // 避免编译器警告
    @SuppressWarnings("unchecked")
    E elementData(int index) {
        // 将数组元素转换为泛型类型 E 并返回
        return (E) elementData[index];
    }

    // 移除指定位置的元素,并将后续元素左移
    public E remove(int index) {
        rangeCheck(index);

        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;
    }

    // 移除指定元素的第一次出现,找到后立即删除。
    public boolean remove(Object o) {
        if (o == null) {
            // 如果元素为 null,遍历 ArrayList 中的所有元素
            for (int index = 0; index < size; index++)
                // 查找第一个为 null 的元素
                if (elementData[index] == null) {
                    // 删除指定下标位的元素
                    fastRemove(index);
                    return true;
                }
        } else {
            // 如果元素不为 null,遍历 ArrayList 中的所有元素
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }

    // 通过自身拷贝改变原先索引,将要删除的索引元素置空
    private void fastRemove(int 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
    }

    // 请为该方法的实现步骤添加注释
    public void clear() {
        for (int i = 0; i < size; i++)
            elementData[i] = null;

        size = 0;
    }

    // 检查索引是否越界
    private void rangeCheck(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException();
    }
}

你可能感兴趣的:(Java,java)