1. 程式人生 > >Java集合(一)-ArrayList原始碼解析

Java集合(一)-ArrayList原始碼解析

ArrayList是什麼?

ArrayList是Java集合中的一份子,它的內部結構實為陣列並封裝了一些方法和特性方便使用者,為什麼不用陣列呢?因為ArrayList更加方便:如果你再不確定元素個數的情況下建立一個數組,那麼在陣列容量不夠的情況下需要手動擴容(也就是重新初始化一個數組),但是在ArrayList中會在內部自動擴容。ArrayList的特性還很多,都是為了使用方便,在下面的講解中你們可以邊看邊細細思考。

我們通過原始碼的方式來講解ArrayList以便於讓你更加深入地學習。

構造器

ArrayList提供三個構造器,分別提供於無參初始化、指定容量初始化、指定提供元素的集合引數初始化:

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

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

ArrayList定義了三個陣列來根據不同的情況建立陣列,我們可以看到陣列型別是 Object 型別。 第一個構造器需要傳入一個 int 型的變數來規定初始化陣列的大小,在建構函式裡會對你傳入的值進行檢測,若果大於0的話就用 elementData 來初始化,注意,這是一個反序列化變數;如果你傳入的大小是0的話用初始化好的容量為0的EMPTY_ELEMENTDA

第二個構造器是預設無參的構造器,它會建立一個長度為0的 Object 陣列儲存元素,也就是 DEFAULTCAPCITY_EMPTY_ELEMENT陣列。

第三個構造器是傳入一個一個集合物件來初始化 ArrayList 的,它會將傳入元素的元素全部複製給 ArrayList ,我們可以看到,如果傳入集合不為空的話會呼叫 Arrays.copyOf() 方法將元素複製到 ArrayList() 方法中,那麼這個 Arrays.copyOf() 方法是幹什麼的呢?看看原始碼:

    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        @SuppressWarnings("unchecked")
        T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

其實它在內部呼叫了 System.arraycopy() 方法,這是一個本地方法,是用C/C++寫的,就不做贅述了。

屬性 DEFAULT_CAPACITY 和 size:

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

    /**
     * The size of the ArrayList (the number of elements it contains).
     *
     * @serial
     */
    private int size;

這兩個屬性容易混淆,size值是指容器中時間有多少個元素;

DEFAULT_CAPACITY 表達的是 ArrayList 中儲存元素的 Object 陣列的預設長度,但它不代表你的容器容量,也不是說你的容器在初始化的時候容量都是10。在上面的構造器中我們能看到如果在傳入容量大小的構造其中傳入0的話它和預設無參的構造器使用的陣列不是一個數組,為什麼呢?

在原始碼開頭 ArrayList 定義了三個陣列來根據不同的情況儲存元素:

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

elementData 是在傳入的容量值不為 0 或者傳入的集合不為空的時候用來儲存元素的;

DEFAULTCAPCITY_EMPTY_ELEMENTDATA 是在呼叫預設無參建構函式的時候用來儲存元素的;

EMPTY_ELEMENT 是用來在傳入容量之為 0 或者傳入集合為空的時候用來儲存元素的;

為什麼要分開儲存 DEFAULTCAPCITY_EMPTY_ELEMENTDATA 和 EMPTY_ELEMENT 呢都用一個數組來儲存不好嗎?

這是因為要根據你的初始化方式不同來踩去不同的擴容機制,如果你使用的是默默人無參建構函式的話,就會用DEFAULTCAPCITY_EMPTY_ELEMENTDATA 儲存元素,那麼在第一次呼叫 add() 方法的時候就會進行擴容,這個時候剛才講到的 DEFAULT_CAPACITY 就派上用場了,會將 ArrayList 的容量擴容為 DEFAULT_CAPACITY 的值也就是 10 。

但是如果你是用傳入容量大小的構造器傳入 0 值或者是使用傳入集合的構造器傳入一個空集合時會用 EMPTY_ELEMENT 儲存元素,這樣的話在第一次呼叫 add() 方法的時候會預設按照它的擴容機制進行第一次擴容,而不是向上面那樣第一次擴容直接擴容到 10。

接下來我們就講講 add() 方法和它的擴容機制:

方法 add():

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++;
    }

    /**
     * A version of rangeCheck used by add and addAll.
     */
    private void rangeCheckForAdd(int index) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

rangeCheck() 方法是為在指定下標處插入元素時檢查下表是否合法;

從上面的原始碼中可以看到兩種 add() 方法在進行插入操作前都會呼叫 ensureCapcityIntermal(size+1) 方法,沒錯這個方法就是檢查在假如一個元素後容器容量(也就是陣列的長度)是否夠用,如果不夠用會呼叫 grow() 方法來擴容:

    private static int calculateCapacity(Object[] elementData, int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            return Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        return minCapacity;
    }

    private void ensureCapacityInternal(int minCapacity) {
        ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
    }

    private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

    /**
     * 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
     */
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

    /**
     * Increases the capacity to ensure that it can hold at least the
     * number of elements specified by the minimum capacity argument.
     *
     * @param minCapacity the desired minimum capacity
     */
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

在上面我們可以看到這一系列的操作的順序是:

(1)calculateCapcity() 根據初始化使用的構造器計算容器應有容量,如果是使用預設無參構造器並且是第一次擴容的話那麼返回容量大小是10,否則返回現在的size+1;

(2)EnsureExplicitCapcity() 判斷計入一個元素後容量是否夠用;

(3)grow() 如果容量不夠用就進行擴容,在方法中可以看到預設擴容大小是現在容量的2/3,也就是每次增加現在容量的1/2。

在第一次擴容的時候如果使用的是預設無參建構函式,會將容量置為 10;若傳入容量為 0 或者傳入空集合來初始化的話,容量增長為 1。

還有一點要注意的是:如果在擴容之後的容量超出了 ArrayList 規定的最大容量,也就是上面的 MAX_ARRAY_SIZE 常量的話,那麼就呼叫 hugerCapcity() 方法返回一個 Integer 能儲存的最大值來作為容量。

到此為止,關於 ArrayList 的擴容機制就講完了,關於最上面為什麼要根據構造器的不同來用兩個都是空的 Object 陣列儲存元素大家也就知道是為什麼了吧!

方法 remove():

remove() 方法也分為兩種情況:一種是根據下標刪除對應的元素;一種是根據元素值刪除所匹配到的第一個元素:

    /**
     * Removes the element at the specified position in this list.
     * Shifts any subsequent elements to the left (subtracts one from their
     * indices).
     *
     * @param index the index of the element to be removed
     * @return the element that was removed from the list
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    public E remove(int index) {
        rangeCheck(index);

        modCount++;
        E oldValue = elementData(index);

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work

        return oldValue;
    }

    /**
     * Removes the first occurrence of the specified element from this list,
     * if it is present.  If the list does not contain the element, it is
     * unchanged.  More formally, removes the element with the lowest index
     * <tt>i</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
     * (if such an element exists).  Returns <tt>true</tt> if this list
     * contained the specified element (or equivalently, if this list
     * changed as a result of the call).
     *
     * @param o element to be removed from this list, if present
     * @return <tt>true</tt> if this list contained the specified element
     */
    public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }

    /*
     * Private remove method that skips bounds checking and does not
     * return the value removed.
     */
    private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work
    }

根據下標刪除元素時先判斷傳入的下標值是否合法,如果合法的話刪除元素;而根據元素刪除的時候,在元素不為null值的時候用 equals() 方法比較是否相同,如果相同的話獲取此元素的下標值,再用 fastRemove() 方法根據此下標刪除元素,這個方法不需要判斷下標是否合法,因為所傳入下標的元素一定存在。切記,按照元素刪除的時候只會刪除所匹配到的第一個元素,而之後相同的元素不會被刪除。

ArrayList還有很多基本操作,比如:get() 和 set() 方法分別獲取下標處的元素和修改下標處的元素;clear() 元素清空ArrayList容器等等,如果對ArrayList還有興趣,大家就去看看原始碼吧!

本人是小白大學生一枚,如有不對或者不當之處,請各位前輩指教。