容器類源碼解析系列(一) ArrayList 源碼分析——基於最新Android9.0源碼

ArrayList 源碼分析——基於最新Android9.0源碼

前言

ArrayList 既是開發人員在平常開發過程當中常常會用到的數據處理容器,也是面試場景中常常會被問到的點。包括LinkedList,HashMap,SparseArray等。所以對這些個數據結構的源碼,仍是頗有必要了解一下的。其餘的幾種容器,在後面的文章再作講解。RT,本文主要講解ArrayList。java

要點

  1. ArrayList和Vector很像,內部都是基於數組來實現數據持久化存儲。不一樣的是Vector的數據操做方法加了synchronized關鍵字,來保證同步,是線程安全的;而ArrayList是非線程安全的。在多線程併發環境下須要加鎖來保證同步或者經過Collections.synchronizedList(new ArrayList(...)) 方法來建立一個synchronized的List。
  2. ArrayList是能夠儲存NULL值。
  3. ArrayList的set、get操做,效率很高,由於是數組實現的,時間複雜度是O(1)。
  4. ArrayList內部實現了"fail-fast"機制,當判斷條件 "modCount != expectedModCount" 的時候會出現ConcurrentModificationException。當多個線程對同一個集合的內容進行操做時,就可能會產生fail-fast事件。若在多線程環境下使用fail-fast機制的集合,建議使用「java.util.concurrent包下的類」去取代「java.util包下的類」。
  5. 由於ArrayList的內部是用數組實現數據存儲的,因此會大量出現Arrays.copyOf、System.arraycopy這個兩個方法,來作數據移位處理。

構造

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

    /** * 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. */
    // Android-note: Also accessed from java.util.Collections
    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默認的capacity是10,elemenData是重點,他就是用來儲存數據的;EMPTY_ELEMENTDATA和DEFAULTCAPACITY_EMPTY_ELEMENTDATA它們是區別是一個size是0,一個是10.在看ArrayList的構造函數時就能夠清晰的感覺到它們的區別。面試

/** * 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 */
    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;
    }
複製代碼

經過上面的代碼,瞭解到,若是沒有設置capacity參數的話,會把DEFAULTCAPACITY_EMPTY_ELEMENTDATA的引用傳給elementData。若是傳進來的capacity的大小爲0的話,則EMPTY_ELEMENTDATA會被賦給elementData。數組

擴容機制

private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }

        ensureExplicitCapacity(minCapacity);
    }

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

        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
複製代碼
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);
    }
複製代碼

前兩個方法我就不說了,邏輯很簡單,一看就知道。grow這個方法是擴容的核心方法,當須要擴容的時候,先記錄以前的數組大小,新的數組大小是以前的1.5倍,"oldCapacity >> 1"表示右移一位,是除2操做。緊接着是兩個條件判斷來決定最終的擴容後的大小。可是通常這個兩個條件不會走進去的。除非調用了ensureCapacity(int minCapacity),這個方法來自行決定擴容後的大小,那麼就頗有可能走進if (newCapacity - minCapacity < 0)條件裏。而後就是經過安全

Arrays.copyOf微信

方法,建立一個新的數組指向elementData。這就是ArrayList的擴容機制,比較簡單。數據結構

經常使用操做

public E get(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

        return (E) elementData[index];
    }
複製代碼
public E set(int index, E element) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

        E oldValue = (E) elementData[index];
        elementData[index] = element;
        return oldValue;
    }
複製代碼

上面是ArrayList的get,set操做,是否是很簡單,由於是用的數據這個結構來存儲數據,因此,get、set操做,賊方便和迅速。多線程

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) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }
複製代碼
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) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(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;
    }
複製代碼

add系列的操做,在插入數據以前都須要先進行一個是否須要擴容的判斷,若是須要的話,就走以前咱們分析的擴容邏輯。接着呢,若是須要插入指定位置的,也就是須要進行數據移位操做的,經過System.arraycopy方法,來移動數據,接着在指定位置上直接設置進去就行了。你們能夠根據上面的代碼邏輯,本身在腦海裏想象或者在紙上筆畫一下就明白了。併發

public E remove(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

        modCount++;
        E oldValue = (E) 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
    }
複製代碼

remove系列的操做,主要是先經過循環遍歷,定位到要刪除對象的index,而後再經過arraycopy方法,來作數據移動,最後把數據的最後一位數給置爲null,方便GC回收。ide

Fail-Fast機制

1.定義函數

經過ArrayList的iterator(),listIterator()等方法返回的迭代器,這些返回的iterator的方法是fail-fast,即這些iterator被建立了(這是前提),而後若是iterator對應的list發生告終構性的修改,好比:add、remove方法。那麼就會致使ConcurrentModificationException。


可能一開始看這個定義有點不是很理解,咱們來分析分析這個定義。上面的定義咱們抓住了一個主語,iterator,這是主要核心,而後修飾iterator的是:經過ArrayList的iterator(),listIterator()建立的iterator,這是一個點,接着一個注意的點是:是iterator對應的list發生結構性修改,這個含義是,調用了ArrayList的add,remove等方法,並非iterator的remove,next等方法

咱們先看第一個點:

/** * Returns a list iterator over the elements in this list (in proper * sequence). * * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. * * @see #listIterator(int) */
    public ListIterator<E> listIterator() {
        return new ListItr(0);
    }

    /** * Returns an iterator over the elements in this list in proper sequence. * * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. * * @return an iterator over the elements in this list in proper sequence */
    public Iterator<E> iterator() {
        return new Itr();
    }

複製代碼

上面兩個方法,是定義中,ArrayList的兩個建立iterator的方法,咱們看看建立出來的iterator到底有什麼特殊,爲啥子,只要建立了iterator,就頗有可能出現ConcurrentModificationException,即fail-fast現象。

private class Itr implements Iterator<E> {
        // Android-changed: Add "limit" field to detect end of iteration.
        // The "limit" of this iterator. This is the size of the list at the time the
        // iterator was created. Adding & removing elements will invalidate the iteration
        // anyway (and cause next() to throw) so saving this value will guarantee that the
        // value of hasNext() remains stable and won't flap between true and false when elements
        // are added and removed from the list.
        protected int limit = ArrayList.this.size;

        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        public boolean hasNext() {
            return cursor < limit;
        }

        @SuppressWarnings("unchecked")
        public E next() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            int i = cursor;
            if (i >= limit)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();

            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
                limit--;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public void forEachRemaining(Consumer<? super E> consumer) {
            Objects.requireNonNull(consumer);
            final int size = ArrayList.this.size;
            int i = cursor;
            if (i >= size) {
                return;
            }
            final Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length) {
                throw new ConcurrentModificationException();
            }
            while (i != size && modCount == expectedModCount) {
                consumer.accept((E) elementData[i++]);
            }
            // update once at end of iteration to reduce heap write traffic
            cursor = i;
            lastRet = i - 1;

            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }
複製代碼

這個是建立出來的iterator,類比較小,就把代碼全貼了,這個類裏面咱們看到很多拋ConcurrentModificationException異常的代碼。拋出這個異常的條件是什麼呢?

有兩個,一個是:

if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
複製代碼

一個是:

if (i >= elementData.length) {
                throw new ConcurrentModificationException();
            }
複製代碼

咱們看第一個條件,modCount是ArrayList的成員變量,expectedModCount是Itr的成員變量,當Itr被建立的時候,expectedModCount被賦值爲此時的modCount的值,僅在執行Itr的remove方法時會被再次賦值,其餘場景不在變化。可是modCount是ArrayList的成員變量,在ArrayList進行數據結構上的修改的時候就會發生變化,例如:

public E remove(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

        modCount++;
        E oldValue = (E) 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;
    }
複製代碼

modCount++在上面的代碼咱們很明顯的看到,那麼這就會出現問題,假如咱們在建立好iterator了,此刻modCount是5,那expectedModCount固然也是5,而後,咱們執行了ArrayList的remove或者add等會讓modCount的值發生修改的方法,而後modCount值改變了,可是expectedModCount值沒變,那下次再執行Itr(迭代器)裏面的方法就會出現modCount!=expectedModCount的狀況,也就會拋出了ConcurrentModificationException異常。

第二個條件呢,是elementData.length發生的變化,和第一個條件原理是一致,表如今稍有不一樣,就不在細說,可自行分析。

2.注意的點

  • fail-fast問題不只僅會出如今多線程併發的場景下,單線程的狀況下也會出現。上面的分析能夠發現這一點。之因此說這個是由於有的人可能會認爲只有在多線程纔可能發生這種狀況,那是沒有真正搞懂緣由。其實,不盡然。
  • fail-fast的出現時機是不肯定性的,不能夠拿這個做爲開發流程中的一個判斷條件。
  • 併發場景,要作同步,能夠採用java.util.concurrent下的容器類。

3.解決fail-fast問題

既然知道了出現ConcurrentModificationException異常的緣由,那解決的方法就是不知足這個條件就能夠了,解決的方法是開放,多樣的。好比能夠用Itr(迭代器)內部的remove,next等修改結構的方法達處處理數據的目的。

容器類源碼解析系列(二)—— LinkedList 集合源碼分析(最新版)


掃碼加入個人我的微信公衆號:Android開發圈 ,一塊兒學習Android知識!!

在這裏插入圖片描述
相關文章
相關標籤/搜索