ArrayList source code analysis --jdk1.8

ArrayList Overview

  1. ArrayList and expansion index can be dynamically remove redundant sequences dynamic capacity, a collection of array-based implementation.
  2. ArrayList support random access, cloning, sequencing, and ordered elements can be repeated.
  3. ArrayList initial default length 10, using the Object [] stores various types of data.

ArrayList data structure

  The data structure is the essence of a collection of lies, data structures often limit the role and focus of the collection, understand the various data structures we analyze the source code is the only way.
  ArrayList data structure as follows:
ArrayList source code analysis --jdk1.8

ArrayList source code analysis

/*
 * 用数组实现的集合,支持随机访问,元素有序且可以重复
 * RandomAccess(ArrayList) 支持快速随机访问,使用for循环更加快速
 * LinkedList 使用 iterator迭代器更加 快速
 * RandomAccess 这是一个标记接口,一般此标记接口用于 List 实现,以表明它们支持快速(通常是恒定时间)的随机访问。
 * 该接口的主要目的是允许通用算法改变其行为,以便在应用于随机或顺序访问列表时提供良好的性能
 */
public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
 /**
     * 默认长度  10
     */
    private static final int DEFAULT_CAPACITY = 10;

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

    /**
     * ArrayList中的元素  是Object[]类型的数组
     */
    transient Object[] elementData; // non-private to simplify nested class access

    /**
     * 动态数组的实际大小 ,默认为0
     * @serial
     */
    private int size;

         /**
     * 集合长度构造函数
     */
    public ArrayList(int initialCapacity) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
    }

         /**
     * 无参构造函数,设置元素数组为空 注意此时初始容量是0,而不是大家以为的 10
     */
    public ArrayList() {
        super();
        this.elementData = EMPTY_ELEMENTDATA;
    }

    /**
     * 集合参数构造函数
     */
    public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray(); // 转化为数组
        size = elementData.length;
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class) //是否成功转化为Object类型数组
            elementData = Arrays.copyOf(elementData, size, Object[].class); //不为Object数组的话就进行复制
    }

Analysis and implementation inheritance ArrayList
ArrayList source code analysis --jdk1.8
   ArrayList the extends AbstractList
   AbstractList the extends AbstractCollection
  Java classes inherit all Object, so ArrayList inheritance structure as FIG.
   1. AbstractList is an abstract class that implements List <E> interfaces, List <E> List defines the general method, and can have an abstract class AbstractList abstract methods, can also have a specific implementation, the interface is to achieve AbstractList Some common methods to achieve the basic add / get / indexOf / iterator subList / RandomAccessSubList method, ArrayList and then inherit AbstractList, to get the general basis of the method /, and then himself in the realization of some of their own unique way, this benefit is: let code more concise, the bottom of the class inheritance hierarchy are common methods are extracted, to achieve together, reducing code duplication.
   2.ArrayList implements List <E>, RandomAccess, Cloneable , Serializable

Guess you like

Origin blog.51cto.com/hackerxian/2426030