List集合源码解读

一:总述:

  主要讲解3个集合

  1.ArrayList:

   底层是数组,线程不安全;

  2.LinkedList:

   底层是链表,线程不安全;

  3.Vector

     底层数据结构是数组。线程安全;

二:ArrayList解析

  List集合源码解读

  首先,我们来看一下ArrayList的属性:

    /**
    * Default initial capacity.
    */
    private static final int DEFAULT_CAPACITY = 10;//初始化容量值

  
/** * Shared empty array instance used for empty instances. */ private static final Object[] EMPTY_ELEMENTDATA = {};//指定ArrayList的容量为0时,返回该空数组 /** * 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 = {};//与上个属性的区别是:该数组是默认返回的,而上个属性是指定容量为0时返回 /** * 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的实际大小

  根据上面我们可以清晰的发现:ArrayList底层其实就是一个数组,ArrayList中有扩容这么一个概念,正因为它扩容,所以它能够实现“动态”增长

1.2构造方法

    /**
     * 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
     */
  //指定初始化长度initCapacity 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. */
  //否则返回的是:DEFAULTCAPACITY_EMPTY_ELEMENTDATA 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; } }

 

1.3 Add()方法

源码如下:

    /**
     * Appends the specified element to the end of this list.
     *
     * @param e element to be appended to this list
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     */
    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

    /**
     * Inserts the specified element at the specified position in this
     * list. Shifts the element currently at that position (if any) and
     * any subsequent elements to the right (adds one to their indices).
     *
     * @param index index at which the specified element is to be inserted
     * @param element element to be inserted
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }

 

1.3.1 Add(E e)

步骤:

  • 检查是否需要扩容
  • 插入元素

首先,我们来看看这个方法:

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

该方法很短,我们可以根据方法名就猜到他是干了什么:

  • 确认list容量,尝试容量加1,看看有无必要
  • 添加元素

接下来我们来看看这个小容量(+1)是否满足我们的需求:

未完待续

 

上一篇:sudo: no tty present and no askpass program specified 解决方法


下一篇:Maven打包报No goals have been specified for this build.异常解决