1. 下面是ArrayList的继承体系结构
从上图可以看出ArrayList的继承体系:
1. 继承AbstractList,实现List,它是一个数组队列,提供了相关的添加、删除、修改、遍历等功能
2. 实现RandomAccess接口,实现快速随机访问:通过元素的序号快速获取元素对象
3. 实现Cloneable接口, 重写了clone(), 可以进行克隆(浅拷贝)
4. 实现Serialiazable接口, 可以实现序列化
2. ArrayList的数据结构
2.1 重要的全局变量
/** * 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. * 使用默认构造器创建ArrayList的对象时默认使用的一个空数组 */ 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. * ArrayList的底层数组 */ transient Object[] elementData; // non-private to simplify nested class access /** * The size of the ArrayList (the number of elements it contains). * ArrayList中已存在的元素个数 * @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 * ArrayList的允许的最大元素个数 */ private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
2.2 构造器
/** * Constructs an empty list with the specified initial capacity. * 创建一个指定容量的空列表,如果传入的参数为零,这使用一个默认已经创建好的空列表 * 如果参数小于0,这会抛出异常 * @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. * 默认的构造器,初始化容量为0,在第一个元素新增时 * 使用DEFAULT_CAPACITY(10)完成有容量的初始化 */ 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. * 接受一个Collection对象直接转换为ArrayList * @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(); //如果存在元素,且数组中的元素不是Object类型的,则进行转换并赋给ArrayList的底层数组 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; } }
时间: 2024-12-25 06:38:49