Java中ArrayList源码浅析

ArrayList基本使用

public class ArrayListTest {

    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add("5");
        System.out.println(list.get(0));
    }
}

ArrayList继承层次

public class ArrayList extends AbstractList
        implements List, RandomAccess, Cloneable, java.io.Serializable

基本字段

    private static final long serialVersionUID = 8683452581122892189L;

    /**
     * Default initial capacity.
     */
    //默认的初始化容量
    private static final int DEFAULT_CAPACITY = 10;

    /**
     * Shared empty array instance used for empty instances.
     */
     //空数组,会在构造函数中给予0参数的情况下,赋值给elementData
    private static final Object[] EMPTY_ELEMENTDATA = {};

    /**
     * Shared empty array instance used for default sized empty instances. We
     * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
     * first element is added.
     */
    //在一个个元素加入进来的时候,会自动扩展成为DEFAULTCAPACITY
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

    /**
     * The array buffer into which the elements of the ArrayList are stored.
     * The capacity of the ArrayList is the length of this array buffer. Any
     * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
     * will be expanded to DEFAULT_CAPACITY when the first element is added.
     */
    //真正的数组的引用
    transient Object[] elementData; // non-private to simplify nested class access

    /**
     * The size of the ArrayList (the number of elements it contains).
     *
     * @serial
     */
    //添加的元素数量
    private int size;

    /**
     * The maximum size of array to allocate.
     * Some VMs reserve some header words in an array.
     * Attempts to allocate larger arrays may result in
     * OutOfMemoryError: Requested array size exceeds VM limit
     */
     //是因为有header words?才导致要Integer的最大值减8
     //其实这个变量只在后面使用一次,比较了之后,然后还是按照MAX_VALUE来扩容了...为啥
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

构造函数

    /**
     * Constructs an empty list with the specified initial capacity.
     *
     * @param  initialCapacity  the initial capacity of the list
     * @throws IllegalArgumentException if the specified initial capacity
     *         is negative
     */
    //带有初始化容量的构造函数
    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            //初始化一个initialCapacity大小的Object数组
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            //若初始化为0,则使用默认的空数组赋值
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            //若为负数,抛出非法参数异常
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }

    /**
     * Constructs an empty list with an initial capacity of ten.
     */
    //最基本的构造函数
    public ArrayList() {
        //注意这里被赋值为DEFAULTCAPACITY_EMPTY_ELEMENTDATA
        //而不是EMPTY_ELEMENTDATA,表示的意思就是当add的时候
        //会默认扩容为DEFAULT_CAPACITY
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

    /**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public ArrayList(Collection c) {
        elementData = c.toArray();
        if ((size = elementData.length) != 0) {
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        } else {
            // replace with empty array.
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }

add操作

    /**
     * Appends the specified element to the end of this list.
     *
     * @param e element to be appended to this list
     * @return true (as specified by {@link Collection#add})
     */
    public boolean add(E e) {
        //添加元素的时候,调用内部确保容量的方法
       //为什么是内部呢?因为还有一个共有的ensureCapacity方法
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        //因为本身是一个数组,所以在下一个数组索引位置添加上元素
        elementData[size++] = e;
        return true;
    }
    private void ensureCapacityInternal(int minCapacity) {
        //如果elementDate等于DEFAULTCAPACITY_EMPTY_ELEMENTDATA
        //表示第一次扩容时,起码要扩容至DEFAULT_CAPACITY
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            //取DEFAULT_CAPACITY=10和minCapacity(可以在初始化函数中初始化)的最大值
            //也就是说若调用默认构造函数,第一次会起码扩展为10的大小
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        //确保显示的容量
        ensureExplicitCapacity(minCapacity);
    }

其实ensureCapacityInternal一个任务是算出不论是第一次添加导致的扩容还是后面添加导致的扩容的最小的容量值。然后将这个最小扩容值传递给ensureExplicitCapacity,由ensureExplicitCapacity实现扩容。

    private void ensureExplicitCapacity(int minCapacity) {
        //用于迭代器的fail fast机制
        modCount++;

        // overflow-conscious code
       //如果最小容量大于数组的长度,那么扩容。
       //否则,则不扩容。
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
    /**
     * Increases the capacity to ensure that it can hold at least the
     * number of elements specified by the minimum capacity argument.
     *
     * @param minCapacity the desired minimum capacity
     */
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        //新的容量为旧的容量的1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        //如果扩了1.5倍之后,还是小,那么以最小的容量进行扩容
        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扩展为newCapacity大小,使用复制数组的方式
        elementData = Arrays.copyOf(elementData, newCapacity);
    }
    private static int hugeCapacity(int minCapacity) {
        //如果minCapacity小于0(而minCapacity是由某数+1得到)
        //其实也刚开始想错了,minCapacity也有可能是addAll()调用导致的
        //反正minCapacity是一个需要保证的最小的容量值,不需要去理解
        //其他函数是如何调用的
        //所以minCapacity是由于Integer溢出的
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
       //这里表示将最大容量扩展为Integer.MAX_VALUE,那么MAX_ARRAY_SIZE还有什么意义呢?
        //这里minCapacity没有溢出说明是小于MAX_ARRAY_SIZE,但是分为两种情况,如果小于MAX_ARRAY_SIZE
       //那么就直接扩容为MAX_ARRAY_SIZE
       //否则比如说是MAX_VALUE-2,那么最多扩容为MAX_VALUE
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

get操作

    /**
     * Returns the element at the specified position in this list.
     *
     * @param  index index of the element to return
     * @return the element at the specified position in this list
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    public E get(int index) {
        //判断是否越界
        rangeCheck(index);
        //返回数组的值
        return elementData(index);
    }
    /**
     * Checks if the given index is in range.  If not, throws an appropriate
     * runtime exception.  This method does *not* check if the index is
     * negative: It is always used immediately prior to an array access,
     * which throws an ArrayIndexOutOfBoundsException if index is negative.
     */
    private void rangeCheck(int index) {
        if (index >= size)
            //如果下标大于元素的数量,那么抛出异常
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

remove操作

    /**
     * Removes the element at the specified position in this list.
     * Shifts any subsequent elements to the left (subtracts one from their
     * indices).
     *
     * @param index the index of the element to be removed
     * @return the element that was removed from the list
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    public E remove(int index) {
        rangeCheck(index);
        //add和remove都需要让modCount加一
        modCount++;
        E oldValue = elementData(index);

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        //最大位的引用置为null,让GC回收
        elementData[--size] = null; // clear to let GC do its work
        //返回删除的值
        return oldValue;
    }

参考

自己动手系列——实现一个简单的ArrayList
Arrays.copyof(···)与System.arraycopy(···)区别
Java提高篇(三四)—–fail-fast机制
why-the-maximum-array-size-of-arraylist-is-integer-max-value-8---其实并不是按照Integer.MAX_VALUE - 8来进行的
Java中ArrayList最大容量为什么是Integer.MAX_VALUE-8?---感觉知乎上面理解的对一些
Why I can't create an array with large size?----还有这个Java里面数组最大可以开多大

你可能感兴趣的:(Java中ArrayList源码浅析)