ArrayList實現原理(JDK1.8)

ArrayList實現原理(JDK1.8)

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable

ArrayList 繼承於AbstractList,實現了List接口,其實AbstractList 已經實現過List接口,這裏重複實現使得接口功能更加清晰,JDK中不少類都是如此。java

其中Cloneable接口是克隆標記接口,Serializable序列化標記接口,須要clone和序列化功能必須實現這兩個接口,而RandomAccess,單純是一個標誌接口 ,該接口表示該類支持快速隨機訪問,且在循環遍歷時for循環的方式會優於用迭代器。數組

1.成員變量

// 默認初始容量
   private static final int DEFAULT_CAPACITY = 10;

   // 空數組實例,初始容量爲0或者傳入集合爲空集合(不是null)時使用
   private static final Object[] EMPTY_ELEMENTDATA = {};
   
   // 空數組示例,無參構造時使用
   private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
   
   // ArrayList內部數據容器
   transient Object[] elementData; // non-private to simplify nested class access
   
   // 實際元素數量
   private int size;

在ArrayList中,主要有五個成員變量。DEFAULT_CAPACITY表示初始容量大小,即在咱們初始化ArrayList時不指定容量大小, 默認容量將會是10,Object[] elementData 則是ArrayList內部實際存儲對象的容易,也就是咱們常說的ArrayList是數組實現的。dom

在1.8中,空數組分爲了兩類狀況,EMPTY_ELEMENTDATA 與 DEFAULTCAPACITY_EMPTY_ELEMENTDATA,在標記空數組的時候區分了不一樣的狀況。ui

2.構造方法

ArrayList有三個構造方法,指定容量的ArrayList(int initialCapacity) ,無參構造ArrayList() 以及傳入集合的ArrayList(Collection<? extends E> c)。this

public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

最簡單的莫過於無參構造,直接賦值爲空數組DEFAULTCAPACITY_EMPTY_ELEMENTDATA。其實對於常說的默認容量10,是在第一次添加元素調用add()方法時處理的,並非構造方法中。3d

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

對於傳入容量的構造方法,當傳入參數 > 0時,直接初始化對應容量的數組,參數類型爲int,也即ArrayList的最大初始容量不能超過Integer.MAX_VALUE,事實上ArrayList的最大容量也只能是Integer.MAX_VALUE。而初始容量傳入0,會賦值爲空數組EMPTY_ELEMENTDATA。若是 < 0,這個顯然的不容許了,直接IllegalArgumentException指針

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

集合構造時,沒有進行null校驗,也就是說若是傳入null,直接就會NPE異常。集合構造的邏輯也很簡單,當傳入集合不爲空時,調用Arrays.copyOf進行復制,而且容量 size爲傳入大小,而傳入集合爲空,則賦值爲空數組EMPTY_ELEMENTDATA。code

3.添加元素

ArrayList在添加元素時,都會進行容量確認,可能會涉及到擴容,數組複製,因此效率相對較低。同時在添加元素時,ArrayList並未對元素自己進行校驗,因此是容許集合中存在null的狀況。對象

3.1.尾部添加元素
public boolean add(E e) {
        // 肯定容量
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        // 設值
        elementData[size++] = e;
        return true;
    }

在add()方法中,最主要的是肯定容量ensureCapacityInternal(int minCapacity)方法。blog

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

首先會調用calculateCapacity(Object[] elementData, int minCapacity) 計算容量而後再ensureExplicitCapacity(int minCapacity)

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

這裏僅僅判斷了是不是空數組DEFAULTCAPACITY_EMPTY_ELEMENTDATA(== 地址比較),若是前面還有印象的話,這個只會在無參構造時,纔會初始化爲DEFAULTCAPACITY_EMPTY_ELEMENTDATA,這時候會取DEFAULT_CAPACITY(10)與傳入minCapacity的較大值,常說的默認容量大小10也就是在這裏誕生的。

而其餘的狀況,都直接但會minCapacity,也即 size + 1,若是首次添加,那就是1。

private void ensureExplicitCapacity(int minCapacity) {
        modCount++;
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

modCount是一個操做計數器,add與remove都會 + 1。當咱們須要在循環中刪除ArrayList元素時,須要使用迭代器Iterator的remove()方法,此時直接使用List的刪除有針對modCount的校驗,會拋出 ConcurrentModificationException異常。

若是minCapacity大於數組容量,則調用grow(int minCapacity)進行擴容。

private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        // 新容量增加 0.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0) // MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

擴容時,新的容量爲原容量 + 原容量的一半,也就是0.5倍增加。若是增加後的新容量比計算出來的容量minCapacity小,則賦值爲minCapacity,若是大於MAX_ARRAY_SIZE(Integer.MAX_VALUE - 8),則進入hugeCapacity(int minCapacity)方法。

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

這裏能夠看到,當minCapacity < 0 時,會產生OutOfMemoryError,這是一個Error子類,這是須要避免的。何時minCapacity會小於0呢,當ArrayList大小爲Integer.MAX_VALUE後,還須要擴容,則會發生錯誤。

這個方法,咱們能夠看出,當ArrayList須要的容量首次大於MAX_ARRAY_SIZE時,會設置爲MAX_ARRAY_SIZE,而後再次擴容時會變成Integer.MAX_VALUE,若是還不夠,那就會發生錯誤。

擴容的最後一步是調用Arrays.copyOf進行元素的複製,這個最終也是調用System.arraycopy進行操做的。同時size++,實際元素的數量也增長 1。

3.2.中間添加元素
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++;
    }

在中間添加元素的邏輯和尾部添加元素基本同樣。

private void rangeCheckForAdd(int index) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

添加元素前,首先要進行範圍檢查,添加的範圍只能在[0,size]之間,index == size時,其實就是尾部插入。而後確認容量新的容量,這個方法尾部添加時已經講過,接着數組複製,這步複製會跳過index位置的處理,最後再對index位置賦值,即完成了index位置的添加。

能夠看到最後調用了size++,add(int index, E element)方法老是會添加元素,即便該index位置存在數據,只是會將原來的index位置數據日後擠動一位,並不會進行覆蓋。

3.3.批量添加

ArrayList除了add()與add(int index, E element),還有兩個批量添加的方法。

public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        int numNew = a.length;
        // 確認容量
        ensureCapacityInternal(size + numNew);  // Increments modCount
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
        return numNew != 0;
    }

    public boolean addAll(int index, Collection<? extends E> c) {
        // 範圍檢查
        rangeCheckForAdd(index);
        Object[] a = c.toArray();
        int numNew = a.length;
        // 確認容量
        ensureCapacityInternal(size + numNew);  // Increments modCount
        int numMoved = size - index;
        if (numMoved > 0)
            System.arraycopy(elementData, index, elementData, index + numNew, numMoved);
        System.arraycopy(a, 0, elementData, index, numNew);
        size += numNew;
        return numNew != 0;
    }

有了前面單個元素的添加基礎,批量添加就很好懂了,惟一的區別就是在數組複製時,是複製整個待添加的集合。對於index位置的批量添加,中間插入的話(numMoved > 0),第一次複製會騰出中間要添加集合長度的位置,第二次將添加的集合複製到index位置。

4.修改元素

對於ArrayList中元素的修改,若是是對象屬性的修改,能夠直接修改引用對象,但對於基本類型包裝類或者String呢,並無辦法經過引用修改,亦或者咱們要更換對象引用,這時候就須要調用set(int index, E element)。

public E set(int index, E element) {
        // 範圍檢查
        rangeCheck(index);
        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }

這個方法實現很容易,ArrayList的修改本質就是對數組的值進行更改。首先進行範圍檢查,防止數組越界,這個很好理解,ArrayList內部就是數組,而後對index位置的值進行替換便可。

private void rangeCheck(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

elementData(int index)獲取了原來的值,用於set返回值,elementData實現更加簡單,就是數組取值。

5.移除元素

ArrayList中移除元素的方法有三個,按索引刪除remove(int index)、按元素刪除remove(Object o)以及批量刪除removeAll(Collection<?> c)等。

5.1.索引刪除
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;
    }

因爲移除元素,並不涉及內部數組大小變化,因此實現相對較簡單。必需要的範圍檢查,這個已經絲絕不陌生了,而後判斷是不是尾部刪除,若是不是尾部刪除,則進行System.arraycopy複製,複製的目的是將index後的元素向前挪動 1 位元素以覆蓋要刪除的index位置,而後size減 1。

在移除方法中,能夠看到modCount進行增長。同時對移除後尾部的元素賦值爲null了,讓GC生效。

5.2.按元素刪除
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;
    }

按元素刪除的時候,首先判斷了元素是否爲null,由於ArrayList中是能夠添加null的,這裏不一樣分支的邏輯是同樣的,都是遍歷集合比較是否和傳入元素相同,只是比較一個是 == null 一個是 equals。若是相同則刪除,而後return了,因此remove(Object o)方法只會刪除集合第一個與傳入對象相同的元素。

重點就是這個fastRemove了。

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
    }

看到這個方法第一感受是什麼?是否是似曾相識,沒錯,fastRemove和按指針刪除基本上市同樣的,只是少了範圍校驗和獲取刪除前的元素這兩步。

5.3.批量刪除
public boolean removeAll(Collection<?> c) {
        Objects.requireNonNull(c);
        return batchRemove(c, false);
    }

對於removeAll(Collection< ? > c),校驗非空後調用了batchRemove(Collection< ? > c, boolean complement)。

private boolean batchRemove(Collection<?> c, boolean complement) {
        final Object[] elementData = this.elementData;
        int r = 0, w = 0;
        boolean modified = false;
        try {
            for (; r < size; r++)
                // 找出不須要移除的元素,放在數組的前面
                if (c.contains(elementData[r]) == complement)
                    elementData[w++] = elementData[r];
        } finally {
            // Preserve behavioral compatibility with AbstractCollection,
            // even if c.contains() throws.
            if (r != size) {
                System.arraycopy(elementData, r, elementData, w, size - r);
                w += size - r;
            }
            if (w != size) {
                // clear to let GC do its work
                for (int i = w; i < size; i++)
                    elementData[i] = null;
                modCount += size - w;
                size = w;
                modified = true;
            }
        }
        return modified;
    }

這個方法看着可能有一點點繞,但明白其原理後就很清晰了,首先遍歷數組,找出在要移除數組中不包含的元素,從原數組頭部開始放,這樣的數有w個,即最終數組前w個元素都是在集合c中包含的,而剩下的位置的元素則不關心,最後就是講w到size的元素賦值爲null,以便GC工做。

6.循環刪除

前面也提到了,ArrayList在循環刪除時會報錯,這個到底是怎麼回事呢?

若是咱們想刪除一個集合中所有的某一個元素,例以下面集合ss中的a元素。

List<String> ss = new ArrayList<>();
        ss.add("a");
        ss.add("b");
        ss.add("a");
        ss.add("b");
        ss.add("c");

當咱們須要刪除一個時,咱們能夠調用remove方法刪除,根據索引或者根據元素都用,可是多個時,咱們不知道每個元素的索引,而根據值也不知道有多少個a存在,因此咱們須要遍歷集合。

這時候就可能存在問題了。

for (String s : ss) {
            if("a".equals(s)){
                ss.remove(s);
            }
        }

不管是fori的仍是foreach的刪除,都會拋出java.util.ConcurrentModificationException,這是由於Arraylist循環時每一次取值都會調用其內部類Itr.next()方法。

public E next() {
            // 校驗modCount
            checkForComodification();
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }

在該方法最開始的地方,有校驗modCount的checkForComodification()方法,這個方法中比較了modCount和expectedModCount,不相等就會拋出ConcurrentModificationException異常。

final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }

那expectedModCount究竟是什麼,爲何和modCount不相等呢。

private class Itr implements Iterator<E> {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

expectedModCount是Itr的成員變量,這個在進行循環時會初始化賦值爲modCount,最開始的時候他們是相等的,通過前面的探究,咱們已經知道在remove調用時modCount會自增,因此checkForComodification就會拋出異常。

而咱們常使用的這個作法就是使用 Itr 的remove。

Iterator<String> it = ss.iterator();
        while (it.hasNext()){
            if("a".equals(it.next())){
                it.remove();
            }
        }

這樣刪除時就沒有任何問題了,這是由於 Itr 的remove中,對expectedModCount進行了從新賦值,使得每一次調用後值都相等。

public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();
            try {
                // 調用ArrayList的刪除
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                // expectedModCount從新賦值
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

7.其餘方法

ArrayList中主要的就是構造方法、add和remove了,這幾個方法看懂後,其餘方法實現就比較清晰了。

好比get方法,其實就是根據索引獲取了數組的元素。

public E get(int index) {
        // 範圍檢查
        rangeCheck(index);
        // 從數組獲取值,即 elementData[index]
        return elementData(index);
    }

例如size方法, 就是返回了size屬性的值。

public int size() {
        return size;
    }

而isEmpty方法,就是判斷size是否爲0.

public boolean isEmpty() {
        return size == 0;
    }

在ArrayList中,有一個獲取子集合的subList方法,這個方法返回的是一個內部類SubList,該類並沒從新建立新的數組,依舊持有了ArrayList數組的元素的引用,因此當修改ArrayList元素的時候,SubList的元素也會跟着修改,這個在實際開發中必定要注意。

public List<E> subList(int fromIndex, int toIndex) {
        subListRangeCheck(fromIndex, toIndex, size);
        return new SubList(this, 0, fromIndex, toIndex);
    }
相關文章
相關標籤/搜索