码迷,mamicode.com
首页 > 编程语言 > 详细

java集合之ArrayList源码解读

时间:2018-01-24 14:05:08      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:tcap   flow   explicit   扩容   cio   xpl   trim   not   mod   

源自:jdk1.8.0_121
ArrayList继承自AbstractList,实现了ListRandomAccessCloneableSerializable

变量

    // 默认容量为10
    private static final int DEFAULT_CAPACITY = 10;

    // 空数组
    private static final Object[] EMPTY_ELEMENTDATA = {};

    // 默认空数组
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

    // 存放数据的数组,被transient修饰的参数不会被序列化
    transient Object[] elementData;

    // 实际元素的大小
    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;
    }

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

toArray()实现方式的不同

注:ArrayList(Collection<? extends E> c) 之所以要判断是否为Object类型,是因为调用toArray()方法的实现方式不同。

java.util.ArrayList中,toArray()返回的是Object数组。

    public Object[] toArray() {
        return Arrays.copyOf(elementData, size);
    }

java.util.Arrays中,有个名为ArrayList的内部类,当调用Arrays.asList()时,返回的是java.util.Arrays$ArrayList内部类对象,而并非java.util.ArrayListjava.util.Arrays$ArrayList中的toArray()返回的虽然是Object数组,但是它的真实类型是个泛型数组。

    private final E[] a;

    public Object[] toArray() {
        return a.clone();
    }

ArrayList扩容

    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

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

        ensureExplicitCapacity(minCapacity);
    }

    private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

    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.5倍的elementData.length小于10,会将elementData的大小扩容成默认的10,反之,则会以1.5倍的elementData.length进行扩容。

将elementData设置为实际容量,动态扩充的多余容量将被删除

    public void trimToSize() {
        modCount++;
        if (size < elementData.length) {
            elementData = (size == 0)
              ? EMPTY_ELEMENTDATA
              : Arrays.copyOf(elementData, size);
        }
    }

java集合之ArrayList源码解读

标签:tcap   flow   explicit   扩容   cio   xpl   trim   not   mod   

原文地址:https://www.cnblogs.com/jarjune/p/8341003.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!