Source Reading List collection

A: Overview:

  Mainly on the three collections

  1.ArrayList:

   The bottom is an array, thread safe;

  2.LinkedList:

   The bottom layer is a linked list, the thread unsafe;

  3.Vector

     Underlying data structure is an array. Thread-safe;

Two: ArrayList resolved

  

  First, let's look at the ArrayList properties:

    / **
    . * The Default Initial Capacity
    * /
    Private Final static int DEFAULT_CAPACITY = 10; // initial capacity value

  
/ ** * Used for instance the Shared empty empty Array instances. * / Private static Final Object [] EMPTY_ELEMENTDATA = {}; // 0 specified, returns the empty capacity of ArrayList array / ** * Used for instance the Shared empty Array . We instances default sized empty * distinguish from EMPTY_ELEMENTDATA to know the this How much to the inflate When * First Element iS added. * / Private static Final Object [] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; // with the distinguishing attributes are: the array is return to the default, and the capacity is designated attribute is zero return / ** * Array of the Elements of Buffer the INTO Which are the ArrayList the Stored. * iS of the capacity of the ArrayList the length of the this Array the Buffer. the 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的实际大小

  According to the above we can clearly find: ArrayList bottom is actually an array, ArrayList expansion have such a concept, precisely because it is expansion, so it can achieve "dynamic" growth

Constructor 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 () method

Source as follows:

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

step:

  • Check if the capacity is needed
  • Insert elements

First, let's take a look at this method:

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

This method is very short, we can guess the name of the method is what he did:

  • Confirmed list capacity, capacity plus one try, to see whether the necessary
  • Adding elements

Next we look at the small capacity (+1) meets our needs:

To be continued

 

Guess you like

Origin www.cnblogs.com/xhlwjy/p/11247186.html