【ArrayList源码】ArrayList构造方法

JDK1.8

ArrayList简介

ArrayList称为数组链表,它是继承AbstractList,并实现了List、RandomAccess、Cloneable和Serializable接口

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
}

ArrayList一共有三个构造方法,三个构造方法如下:

/**
     * 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) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }

    /**
     * Constructs an empty list with an initial capacity of ten.
     */
    public ArrayList() {
        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;
        }
    }

下面逐步分析ArrayList的构造方法中做了哪些事情。

1 无参构造方法

/**
     * 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.
     * ArrayList的缓冲数组,里面存储的是ArrayList数据。ArrayList的容量长度就是这个数组的长度,任何空的使用elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA初始化的ArrayList在添加第一个元素时都会被扩容为DEFAULT_CAPACITY (10)
     */
    transient Object[] 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.
     * 用于默认大小的空的共享空数组实例。我们将此与EMPTY_ELEMENTDATA区分开来,以便在添加第一个元素时知道需要扩张多少。
     */
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
 /**
     * Constructs an empty list with an initial capacity of ten.
     * 构造一个初始容量为10的空list。
     */
    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

看了上面的源码的注释,才知道,ArrayList的无参构造函数里ArrayList的初始容量为空,真正初始化容量的地方是在添加第一个元素的时候。

2 一个参数的构造方法,参数为初始的容量

/**
     * Shared empty array instance used for empty instances.
     * 空的共享数组实例
     */
    private static final Object[] EMPTY_ELEMENTDATA = {};
    /**
     * Constructs an empty list with the specified initial capacity.
     * 构造一个指定容量的空List
     *
     * @param  initialCapacity  the initial capacity of the list
     * initialCapacity是list的初始容量
     * @throws IllegalArgumentException if the specified initial capacity is negative
     * 如果指定容量为负数,则抛出异常
     */
    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {//#1
            this.elementData = new Object[initialCapacity];//#2
        } else if (initialCapacity == 0) {//#3
            this.elementData = EMPTY_ELEMENTDATA;//#4
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);//#5
        }
    }
  • 第#1行如果指定容量大于0,则进行#2操作
  • 第#2行使用指定容量初始化ArrayList的数组。
  • 第#3行如果指定容量为0,则进行#4
  • 第#4行初始化elementData为空数组
  • 第#5行如果指定容量小于0,则抛出异常

3 参数为集合的构造方法

/**
     * 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();//#1
        if ((size = elementData.length) != 0) {//#2
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            if (elementData.getClass() != Object[].class)//#3
                elementData = Arrays.copyOf(elementData, size, Object[].class);//#4
        } else {
            // replace with empty array.
            this.elementData = EMPTY_ELEMENTDATA;//#5
        }
    }
  • 第#1行将参数集合转化为数组并赋值给elementData
  • 第#2行如果elementData的长度(size指的是数组中数量的个数)不等于0,则进行#3,否则进行#5
  • 第#3行如果elementData不是Object数组类型,则执行#4
  • 第#4行将数据赋值到Object数组类型的elementData
  • 第#5行如果elementData长度为0,初始化elementData为空数组

4 总结

以下针对构造函数进行总结

  • ArrayList就是一个数组,在ArrayList里是通过Object[] elementData来存储数据的。
  • ArrayList初始化时容量为0(使用空构造方法初始化),它在添加第一个元素(调用add方法)时如果ArrayList容量为0,则会扩容,扩容为默认大小10个单位。
发布了107 篇原创文章 · 获赞 142 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/u013293125/article/details/95617144