ArrayList的實現原理
1. ArrayList概述:
ArrayList是List接口的可變數組的實現。實現了所有可選列表操作,并允許包括 null 在內的所有元素。除了實現 List 接口外,此類還提供一些方法來操作內部用來存儲列表的數組的大小。
每個ArrayList實例都有一個容量,該容量是指用來存儲列表元素的數組的大小。它總是至少等于列表的大小。隨著向ArrayList中不斷添加元素,其容量也自動增長。自動增長會帶來數據向新數組的重新拷貝,因此,如果可預知數據量的多少,可在構造ArrayList時指定其容量。在添加大量元素前,應用程序也可以使用ensureCapacity操作來增加ArrayList實例的容量,這可以減少遞增式再分配的數量。
注意,此實現不是同步的。如果多個線程同時訪問一個ArrayList實例,而其中至少一個線程從結構上修改了列表,那么它必須保持外部同步。
2. ArrayList的實現:
對于ArrayList而言,它實現List接口、底層使用數組保存所有元素。其操作基本上是對數組的操作。下面我們來分析ArrayList的源代碼:
1) 底層使用數組實現:
private transient Object[] elementData;
2) 構造方法:
ArrayList提供了三種方式的構造器,可以構造一個默認初始容量為10的空列表、構造一個指定初始容量的空列表以及構造一個包含指定collection的元素的列表,這些元素按照該collection的迭代器返回它們的順序排列的。
1 public ArrayList() { 2 this(10); 3 } 4 5 public ArrayList(int initialCapacity) { 6 super(); 7 if (initialCapacity < 0) 8 throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); 9 this.elementData = new Object[initialCapacity]; 10 } 11 12 public ArrayList(Collection<? extends E> c) { 13 elementData = c.toArray(); 14 size = elementData.length; 15 // c.toArray might (incorrectly) not return Object[] (see 6260652) 16 if (elementData.getClass() != Object[].class) 17 elementData = Arrays.copyOf(elementData, size, Object[].class); 18 }
3) 存儲:
ArrayList提供了set(int index, E element)、add(E e)、add(int index, E element)、addAll(Collection<? extends E> c)、addAll(int index, Collection<? extends E> c)這些添加元素的方法。下面我們一一講解:
1 // 用指定的元素替代此列表中指定位置上的元素,并返回以前位于該位置上的元素。 2 public E set(int index, E element) { 3 RangeCheck(index); 4 5 E oldValue = (E) elementData[index]; 6 elementData[index] = element; 7 return oldValue; 8 } 9 10 // 將指定的元素添加到此列表的尾部。 11 public boolean add(E e) { 12 ensureCapacity(size + 1); 13 elementData[size++] = e; 14 return true; 15 } 16 17 // 將指定的元素插入此列表中的指定位置。 18 // 如果當前位置有元素,則向右移動當前位于該位置的元素以及所有后續元素(將其索引加1)。 19 public void add(int index, E element) { 20 if (index > size || index < 0) 21 throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); 22 // 如果數組長度不足,將進行擴容。 23 ensureCapacity(size+1); // Increments modCount!! 24 // 將 elementData中從Index位置開始、長度為size-index的元素, 25 // 拷貝到從下標為index+1位置開始的新的elementData數組中。 26 // 即將當前位于該位置的元素以及所有后續元素右移一個位置。 27 System.arraycopy(elementData, index, elementData, index + 1, size - index); 28 elementData[index] = element; 29 size++; 30 } 31 32 // 按照指定collection的迭代器所返回的元素順序,將該collection中的所有元素添加到此列表的尾部。 33 public boolean addAll(Collection<? extends E> c) { 34 Object[] a = c.toArray(); 35 int numNew = a.length; 36 ensureCapacity(size + numNew); // Increments modCount 37 System.arraycopy(a, 0, elementData, size, numNew); 38 size += numNew; 39 return numNew != 0; 40 } 41 42 // 從指定的位置開始,將指定collection中的所有元素插入到此列表中。 43 public boolean addAll(int index, Collection<? extends E> c) { 44 if (index > size || index < 0) 45 throw new IndexOutOfBoundsException( 46 "Index: " + index + ", Size: " + size); 47 48 Object[] a = c.toArray(); 49 int numNew = a.length; 50 ensureCapacity(size + numNew); // Increments modCount 51 52 int numMoved = size - index; 53 if (numMoved > 0) 54 System.arraycopy(elementData, index, elementData, index + numNew, numMoved); 55 56 System.arraycopy(a, 0, elementData, index, numNew); 57 size += numNew; 58 return numNew != 0; 59 }
4) 讀取:
// 返回此列表中指定位置上的元素。 public E get(int index) { RangeCheck(index); return (E) elementData[index]; }
5) 刪除:
ArrayList提供了根據下標或者指定對象兩種方式的刪除功能。如下:
1 // 移除此列表中指定位置上的元素。 2 public E remove(int index) { 3 RangeCheck(index); 4 5 modCount++; 6 E oldValue = (E) elementData[index]; 7 8 int numMoved = size - index - 1; 9 if (numMoved > 0) 10 System.arraycopy(elementData, index+1, elementData, index, numMoved); 11 elementData[--size] = null; // Let gc do its work 12 13 return oldValue; 14 } 15 16 // 移除此列表中首次出現的指定元素(如果存在)。這是應為ArrayList中允許存放重復的元素。 17 public boolean remove(Object o) { 18 // 由于ArrayList中允許存放null,因此下面通過兩種情況來分別處理。 19 if (o == null) { 20 for (int index = 0; index < size; index++) 21 if (elementData[index] == null) { 22 // 類似remove(int index),移除列表中指定位置上的元素。 23 fastRemove(index); 24 return true; 25 } 26 } else { 27 for (int index = 0; index < size; index++) 28 if (o.equals(elementData[index])) { 29 fastRemove(index); 30 return true; 31 } 32 } 33 return false; 34 }
注意:從數組中移除元素的操作,也會導致被移除的元素以后的所有元素的向左移動一個位置。
6) 調整數組容量:
從上面介紹的向ArrayList中存儲元素的代碼中,我們看到,每當向數組中添加元素時,都要去檢查添加后元素的個數是否會超出當前數組的長度,如果超出,數組將會進行擴容,以滿足添加數據的需求。數組擴容通過一個公開的方法ensureCapacity(int minCapacity)來實現。在實際添加大量元素前,我也可以使用ensureCapacity來手動增加ArrayList實例的容量,以減少遞增式再分配的數量。
public void ensureCapacity(int minCapacity) { modCount++; int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { Object oldData[] = elementData; int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity < minCapacity) newCapacity = minCapacity; // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } }
從上述代碼中可以看出,數組進行擴容時,會將老數組中的元素重新拷貝一份到新的數組中,每次數組容量的增長大約是其原容量的1.5倍。這種操作的代價是很高的,因此在實際使用時,我們應該盡量避免數組容量的擴張。當我們可預知要保存的元素的多少時,要在構造ArrayList實例時,就指定其容量,以避免數組擴容的發生?;蛘吒鶕嶋H需求,通過調用ensureCapacity方法來手動增加ArrayList實例的容量。
ArrayList還給我們提供了將底層數組的容量調整為當前列表保存的實際元素的大小的功能。它可以通過trimToSize方法來實現。代碼如下:
public void trimToSize() { modCount++; int oldCapacity = elementData.length; if (size < oldCapacity) { elementData = Arrays.copyOf(elementData, size); } }
7) Fail-Fast機制:
ArrayList也采用了快速失敗的機制,通過記錄modCount參數來實現。在面對并發的修改時,迭代器很快就會完全失敗,而不是冒著在將來某個不確定時間發生任意不確定行為的風險。具體介紹請參考HashMap的實現原理中的Fail-Fast機制。
8) 關于其他
的一些方法的實現都很簡單易懂,讀者可參照API文檔和源代碼,一看便知,這里就不再多說。
浙公網安備 33010602011771號