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<E> extends AbstractList<E>
        implements List<E>, 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<? extends E> 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 <tt>true</tt> (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里面数组最大可以开多大

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容