您当前的位置: 首页 >  ar

庄小焱

暂无认证

  • 3浏览

    0关注

    800博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

JDK源码——ArrayList类

庄小焱 发布时间:2021-11-22 09:36:33 ,浏览量:3

摘要

博文主要分析Arryalist类相关源码原理。

一、Arraylist的定义

ArrayList 是一个用数组实现的集合,支持随机访问,元素有序且可以重复。ArrayList继承AbstractList 并且实现了List和RandomAccess,Cloneable, Serializable接口。List 接口定义了实现该接口的类都必须要实现的一组方法,如下所示,下面我们会对这一系列方法的实现做详细介绍。

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

二、ArrayList的字段属性

底层的十一个object的数组。

private static final long serialVersionUID = 8683452581122892189L;

    /**
     * Default initial capacity.
     */
    private static final int DEFAULT_CAPACITY = 10;

    /**
     * Shared empty array instance used for empty instances.
     */
    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.
     */
    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;
三、ArrayList的构造函数

此无参构造函数将创建一个 DEFAULTCAPACITY_EMPTY_ELEMENTDATA 声明的数组,注意此时初始容量是0,而不是大家以为的 10。注意:根据默认构造函数创建的集合,ArrayList list = new ArrayList();此时集合长度是0.


    /**
     * Constructs an empty list with an initial capacity of ten.
     */
    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

给定大小有参构造函数


    /**
     * 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);
        }
    }

初始化集合大小创建 ArrayList 集合。当大于0时,给定多少那就创建多大的数组并赋给赋给elementData;当等于0时,将空的数组实例EMPTY_ELEMENTDATA 赋给elementData;当小于0时,抛出异常。

泛型参数有参构造函数:Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素。?是“任意类”的意思,extends继承不多说,E是指定类型。


    /**
     * 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            
关注
打赏
1657692014
查看更多评论
0.0516s