ArrayList原始碼解析
相信寫Java程式碼不久就會接觸到ArrayList,這是個容器類,我們在使用的時候覺得這個容器好像是無限大的一樣,我們可以不斷的操作它(add、get、remove),其實它的內部實現是基於陣列的,這篇文章就是介紹其內部原理。瞭解原理後,我們在使用的時候可以根據實際情況來配置它,讓它擁有更好的效能和更少的記憶體佔用。
ArrayList類主要欄位
private static final long serialVersionUID = 8683452581122892189L; /** * 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. */ 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; /** * 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; 複製程式碼
從這個欄位結構,我們得到以下資訊:
- ArrayList內部主要是使用Object[] elementData來儲存我們新增的資料。
- size欄位記錄當前List已經儲存的資料的個數。
- DEFAULT_CAPACITY欄位標記預設初始化ArrayList時分配的陣列的長度。
- EMPTY_ELEMENTDATA和DEFAULTCAPACITY_EMPTY_ELEMENTDATA是定義的兩個空陣列,為什麼要使用兩個呢?下面我們會分析原因。
ArrayList相關方法解析
下面我們通過我們使用ArrayList時常用的方法來慢慢分析它的原始碼。
建構函式
/** * Constructs an empty list with the specified initial capacity. * * @paraminitialCapacitythe 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; } /** * 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建構函式一共有3個:
- 我們最常用的new ArrayList()其實只是把elementData指向了預設空陣列而已(DEFAULTCAPACITY_EMPTY_ELEMENTDATA)。
- initialCapacity容量的建構函式原始碼也很簡單,如果initialCapacity>0就建立一個initialCapacity容量的陣列,如果initialCapacity==0,elementData只是指向EMPTY_ELEMENTDATA。
-
最後的建構函式是使用Collection集合來初始化ArrayList。先將Collection集合轉化成陣列,然後根據size來執行不同的操作。如果轉化後的陣列不是Object型別的,就重新建立一個size大小的陣列。
下面我們就從常用的方法來一步步解讀ArrayList的原始碼。
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++; } 複製程式碼
從上面我們看到,add(E e)方法直接將元素新增到陣列末尾。而add(int index,E element)方法將元素新增到指定的index位置,當然原先index後面的元素需要調整位置(都往後挪一個位置)。
我們從上面的原始碼中看到ensureCapacityInternal方法,繼續往下看原始碼:
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); } /** * 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:
我們先看ensureCapacityInternal方法。第一步:如果elementData指向DEFAULTCAPACITY_EMPTY_ELEMENTDATA,那麼就擴容陣列到DEFAULT_CAPACITY(10)。通過這一步我們知道只有我們new ArrayList()的時,elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA條件才會成立。第二步:我們根據流程ensureCapacityInternal-->ensureExplicitCapacity-->grow,我們知道這種情況下ArrayList會建立一個長度為10的陣列。 情況2:
上面分析ArrayList構造器時,elementData也會指向EMPTY_ELEMENTDATA空陣列。只有new ArrayList(0)或者new ArrayList(空集合)才會成立。這種情況下,我們根據ensureCapacityInternal-->ensureExplicitCapacity-->grow流程來看,這時候分配的陣列很小(佔用記憶體小),這是保守的記憶體分配策略。 總結:
通過上面的分析,我們知道了DEFAULTCAPACITY_EMPTY_ELEMENTDATA和EMPTY_ELEMENTDATA兩個空陣列的不同用途。前者預設建立10個元素的陣列,然後在這個基礎上進行擴容。後者是比較保守的記憶體分配策略,資料擴容比較緩慢。
grow方法解析
grow方法是整個ArrayList擴容的核心,下面我們來看下其原始碼:
/** * 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; } 複製程式碼
我們看到ArrayList預設擴容大小是原大小的1.5倍。下面邏輯依次是判斷一些邊界的情況:
- 如果newCapacity小於minCapacity,那麼就將陣列大小調整為minCapacity大小。
- 如果minCapacity的值在MAX_ARRAY_SIZE和Integer.MAX_VALUE之間,那麼新陣列分配Integer.MAX_VALUE大小,否則分配MAX_ARRAY_SIZE。
contains,indexOf,lastIndexOf相關檢索方法
/** * Returns <tt>true</tt> if this list contains the specified element. * More formally, returns <tt>true</tt> if and only if this list contains * at least one element <tt>e</tt> such that * <tt>(o==null ? e==null : o.equals(e))</tt>. * * @param o element whose presence in this list is to be tested * @return <tt>true</tt> if this list contains the specified element */ public boolean contains(Object o) { return indexOf(o) >= 0; } /** * Returns the index of the first occurrence of the specified element * in this list, or -1 if this list does not contain the element. * More formally, returns the lowest index <tt>i</tt> such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, * or -1 if there is no such index. */ public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; } /** * Returns the index of the last occurrence of the specified element * in this list, or -1 if this list does not contain the element. * More formally, returns the highest index <tt>i</tt> such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, * or -1 if there is no such index. */ public int lastIndexOf(Object o) { if (o == null) { for (int i = size-1; i >= 0; i--) if (elementData[i]==null) return i; } else { for (int i = size-1; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1; } 複製程式碼
這些方法邏輯都比較清晰,就是迴圈遍歷,找出符合條件的元素而已。
toArray方法
這個方法有時我們需要用到,它是將ArrayList轉化成陣列。下面我們來看其原始碼:
public Object[] toArray() { return Arrays.copyOf(elementData, size); } @SuppressWarnings("unchecked") public <T> T[] toArray(T[] a) { if (a.length < size) // Make a new array of a's runtime type, but my contents:' return (T[]) Arrays.copyOf(elementData, size, a.getClass()); System.arraycopy(elementData, 0, a, 0, size); if (a.length > size) a[size] = null; return a; } 複製程式碼
通過Arrays.copyOf和System.arraycopy方法,ArrayList將elementData陣列中的資料拷貝到新陣列,然後返回。System.arraycopy方法效率很高,其內部使用C/C++(設定會使用匯編),我們平時開發的時候,有陣列拷貝,也應該使用這些方法。
get/set方法
@SuppressWarnings("unchecked") E elementData(int index) { return (E) elementData[index]; } /** * Returns the element at the specified position in this list. * * @paramindex index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E get(int index) { rangeCheck(index); return elementData(index); } /** * Replaces the element at the specified position in this list with * the specified element. * * @param index index of the element to replace * @param element element to be stored at the specified position * @return the element previously at the specified position * @throws IndexOutOfBoundsException {@inheritDoc} */ public E set(int index, E element) { rangeCheck(index); E oldValue = elementData(index); elementData[index] = element; return oldValue; } 複製程式碼
邏輯比較簡單,不做詳細介紹。
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 ? get(i)==null : 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)/或者remove(object)後,需要呼叫System.arraycopy來高效的移動index後面的陣列,讓其可以填充位置。
removeAll/retainAll方法
有的朋友可能沒有用過這兩個方法,下面我們通過一個小栗子來看一下這兩個方法到底是什麼,請看程式碼:
ArrayList<String> list=new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); list.add("F"); ArrayList<String> list1=new ArrayList<>(); list1.add("C"); list1.add("D"); //list.removeAll(list1); //[A, B, E, F] System.out.println(list); list.retainAll(list1); //[C,D] System.out.println(list); 複製程式碼
從結果我們可以看出,removeAll方法是計算兩個集合的差集,retainAll計算兩個集合的交集。下面我們通過原始碼來分析:
public boolean removeAll(Collection<?> c) { Objects.requireNonNull(c); return batchRemove(c, false); } public boolean retainAll(Collection<?> c) { Objects.requireNonNull(c); return batchRemove(c, true); } 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; } 複製程式碼
我們看到,這邊設計比較精妙,通過一個boolean值,將取差值和取交集的方法整合為一個方法。batchRemove方法的程式碼設計實現還是很不錯的,核心的邏輯就是try語句塊裡面的for迴圈,finally語句塊裡面主要是資料拷貝及特殊值的處理。
迭代器
Java集合在設計的時候就是支援迭代器的。下面我們來看看ArrayList裡面迭代器的相關部分。
獲取迭代器物件
/** * 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(); } 複製程式碼
我們看到預設返回的是一個Itr物件,熟悉Java集合層次結構(類繼承結構)的朋友,可能知道ArrayList的基類AbstractList裡面就有一個內部類Itr。現在ArrayList內部重新實現了一個優化版本的Itr類,我們來看原始碼:
/** * An optimized version of AbstractList.Itr */ 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; public boolean hasNext() { return cursor != size; } @SuppressWarnings("unchecked") public E next() { 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]; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } 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; checkForComodification(); } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } } 複製程式碼
從上面的原始碼中,迭代器只提供基本的向後遍歷、刪除等功能。這樣我們在遍歷ArrayList的時候,還可以使用迭代器來進行遍歷(當然for(E e : elements)這種寫法會被編譯期自動轉化成迭代器的呼叫)。
ListItr迭代器
繼續往下研究ArrayList的原始碼,我們會發現ArrayList內部還實現了ListItr的迭代器。這個迭代器除了提供向後遍歷功能外,還提供了向前遍歷,增加、設定等功能。是一個功能比較全的迭代器實現。我們看下原始碼:
/** * An optimized version of AbstractList.ListItr */ private class ListItr extends Itr implements ListIterator<E> { ListItr(int index) { super(); cursor = index; } public boolean hasPrevious() { return cursor != 0; } public int nextIndex() { return cursor; } public int previousIndex() { return cursor - 1; } @SuppressWarnings("unchecked") public E previous() { checkForComodification(); int i = cursor - 1; if (i < 0) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i; return (E) elementData[lastRet = i]; } public void set(E e) { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.set(lastRet, e); } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(E e) { checkForComodification(); try { int i = cursor; ArrayList.this.add(i, e); cursor = i + 1; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } } 複製程式碼
我們看到ListItr這個迭代器裡面很多操作都是直接呼叫的ArrayList類的方法,它只是做了一層封裝。
ConcurrentModificationException異常出現的原因及解決方法
有的朋友在遍歷ArrayList集合的時候可能遇到過這個異常,這個異常時ArrayList類設計的快速失敗機制導致的,這個異常認為集合在遍歷的時候,做出了修改。我下面這個例子就出現了這個異常,一起來看下:
ArrayList<String> list=new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); list.add("F"); for(String item : list) { System.out.println(item); list.remove(item); } //或者 /* Iterator<String> iterator=list.iterator(); while (iterator.hasNext()) { String string = (String) iterator.next(); System.out.println(string); list.remove(string); } */ Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at com.learn.example.RunMain.main(RunMain.java:42) 複製程式碼
下面兩種遍歷方式都會導致異常的發生,下面我們來看下原因。上面介紹過第一種foreach迴圈寫法編譯後就是迭代器。我們直接看迭代器。
@SuppressWarnings("unchecked") public E next() { 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]; } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } 複製程式碼
我們看到next()方法第一行會呼叫checkForComodification()方法,我們看到如果modCount和expectedModCount不相等的話,就會丟擲這個異常。我們上面再看Itr原始碼的時候看到expectedModCount剛開始賦值的是ArrayList類裡面的modCount變數。下面list.remove(o)這個方法裡面會讓modCount++。原始碼如下:
/* * 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 } 複製程式碼
這樣的話就會導致Itr內部的expectedModCount和ArrayList的modCount不一致,從而丟擲這個異常。那麼我們如何解決呢?只需要讓modCount不增加,讓它的值與expectedModCount同步即可。Itr內部也有remove方法,我們呼叫這個方法即可。
Iterator<String> iterator=list.iterator(); while (iterator.hasNext()) { String string = (String) iterator.next(); System.out.println(string); iterator.remove(); } 複製程式碼
總結
- ArrayList內部是Object[]陣列,然後使用的時候進行動態擴容。
- ArrayList預設會分配10個元素的陣列,然後在此基礎上進行擴容,每次新的擴容後的陣列長度是原陣列長度的1.5倍。如果你大概知道集合的容量,可以指定初始化值,減少擴容帶來效能損耗。
- ArrayList適合查詢多,操作少的場景(因為操作過後,絕大部分情況下需要挪動陣列位置)。
- ArrayList集合不是執行緒安全的,在多執行緒環境下需要加鎖或者使用併發安全的容器。