1. ArrayList概述:數組
ArrayList是List接口的可變數組的實現。實現了全部可選列表操做,並容許包括 null 在內的全部元素。除了實現 List 接口外,此類還提供一些方法來操做內部用來存儲列表的數組的大小。
每一個ArrayList實例都有一個容量,該容量是指用來存儲列表元素的數組的大小。它老是至少等於列表的大小。隨着向ArrayList中不斷添加元素,其容量也自動增加。自動增加會帶來數據向新數組的從新拷貝,所以,若是可預知數據量的多少,可在構造ArrayList時指定其容量。在添加大量元素前,應用程序也可使用ensureCapacity操做來增長ArrayList實例的容量,這能夠減小遞增式再分配的數量。
注意,此實現不是同步的。若是多個線程同時訪問一個ArrayList實例,而其中至少一個線程從結構上修改了列表,那麼它必須保持外部同步。併發
關於ArrayList與LinkedList的比較分析:學習
A)ArrayList底層採用數組實現,LinkedList底層採用雙向鏈表實現。this
B)當執行插入或者刪除操做時,採用LinkedList比較好。線程
C)當執行搜索操做時,採用ArrayList比較好。對象
2. ArrayList的實現:blog
對於ArrayList而言,它實現List接口、底層使用數組保存全部元素。其操做基本上是對數組的操做。下面咱們來分析ArrayList的源代碼:索引
1) 底層使用數組實現:接口
Java代碼 ci
- private transient Object[] elementData;
2) 構造方法:
ArrayList提供了三種方式的構造器,能夠構造一個默認初始容量爲10的空列表、構造一個指定初始容量的空列表以及構造一個包含指定collection的元素的列表,這些元素按照該collection的迭代器返回它們的順序排列的。
Java代碼
- public ArrayList() {
- this(10);
- }
-
- public ArrayList(int initialCapacity) {
- super();
- if (initialCapacity < 0)
- throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
- this.elementData = new Object[initialCapacity];
- }
-
- public ArrayList(Collection<? extends E> c) {
- elementData = c.toArray();
- size = elementData.length;
- // c.toArray might (incorrectly) not return Object[] (see 6260652)
- if (elementData.getClass() != Object[].class)
- elementData = Arrays.copyOf(elementData, size, Object[].class);
- }
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)這些添加元素的方法。下面咱們一一講解:
Java代碼
- // 用指定的元素替代此列表中指定位置上的元素,並返回之前位於該位置上的元素。
- public E set(int index, E element) {
- RangeCheck(index);
-
- E oldValue = (E) elementData[index];
- elementData[index] = element;
- return oldValue;
- }
Java代碼
- // 將指定的元素添加到此列表的尾部。
- public boolean add(E e) {
- ensureCapacity(size + 1);
- elementData[size++] = e;
- return true;
- }
Java代碼
- // 將指定的元素插入此列表中的指定位置。
- // 若是當前位置有元素,則向右移動當前位於該位置的元素以及全部後續元素(將其索引加1)。
- public void add(int index, E element) {
- if (index > size || index < 0)
- throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
- // 若是數組長度不足,將進行擴容。
- ensureCapacity(size+1); // Increments modCount!!
- // 將 elementData中從Index位置開始、長度爲size-index的元素,
- // 拷貝到從下標爲index+1位置開始的新的elementData數組中。
- // 即將當前位於該位置的元素以及全部後續元素右移一個位置。
- System.arraycopy(elementData, index, elementData, index + 1, size - index);
- elementData[index] = element;
- size++;
- }
Java代碼
- // 按照指定collection的迭代器所返回的元素順序,將該collection中的全部元素添加到此列表的尾部。
- public boolean addAll(Collection<? extends E> c) {
- Object[] a = c.toArray();
- int numNew = a.length;
- ensureCapacity(size + numNew); // Increments modCount
- System.arraycopy(a, 0, elementData, size, numNew);
- size += numNew;
- return numNew != 0;
- }
Java代碼
- // 從指定的位置開始,將指定collection中的全部元素插入到此列表中。
- public boolean addAll(int index, Collection<? extends E> c) {
- if (index > size || index < 0)
- throw new IndexOutOfBoundsException(
- "Index: " + index + ", Size: " + size);
-
- Object[] a = c.toArray();
- int numNew = a.length;
- ensureCapacity(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;
- }
4) 讀取:
Java代碼
- // 返回此列表中指定位置上的元素。
- public E get(int index) {
- RangeCheck(index);
-
- return (E) elementData[index];
- }
5) 刪除:
ArrayList提供了根據下標或者指定對象兩種方式的刪除功能。以下:
Java代碼
- // 移除此列表中指定位置上的元素。
- public E remove(int index) {
- RangeCheck(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; // Let gc do its work
-
- return oldValue;
- }
Java代碼
- // 移除此列表中首次出現的指定元素(若是存在)。這是應爲ArrayList中容許存放重複的元素。
- public boolean remove(Object o) {
- // 因爲ArrayList中容許存放null,所以下面經過兩種狀況來分別處理。
- if (o == null) {
- for (int index = 0; index < size; index++)
- if (elementData[index] == null) {
- // 相似remove(int index),移除列表中指定位置上的元素。
- fastRemove(index);
- return true;
- }
- } else {
- for (int index = 0; index < size; index++)
- if (o.equals(elementData[index])) {
- fastRemove(index);
- return true;
- }
- }
- return false;
- }
注意:從數組中移除元素的操做,也會致使被移除的元素之後的全部元素的向左移動一個位置。
6) 調整數組容量:
從上面介紹的向ArrayList中存儲元素的代碼中,咱們看到,每當向數組中添加元素時,都要去檢查添加後元素的個數是否會超出當前數組的長度,若是超出,數組將會進行擴容,以知足添加數據的需求。數組擴容經過一個公開的方法ensureCapacity(int minCapacity)來實現。在實際添加大量元素前,我也可使用ensureCapacity來手動增長ArrayList實例的容量,以減小遞增式再分配的數量。
Java代碼
- 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實例時,就指定其容量,以免數組擴容的發生。或者根據實際需求,經過調用ensureCapacity方法來手動增長ArrayList實例的容量。
ArrayList還給咱們提供了將底層數組的容量調整爲當前列表保存的實際元素的大小的功能。它能夠經過trimToSize方法來實現。代碼以下:
Java代碼
- public void trimToSize() {
- modCount++;
- int oldCapacity = elementData.length;
- if (size < oldCapacity) {
- elementData = Arrays.copyOf(elementData, size);
- }
- }
7) Fail-Fast機制:
ArrayList也採用了快速失敗的機制,經過記錄modCount參數來實現。在面對併發的修改時,迭代器很快就會徹底失敗,而不是冒着在未來某個不肯定時間發生任意不肯定行爲的風險。具體介紹請參考我以前的文章深刻Java集合學習系列:HashMap的實現原理 中的Fail-Fast機制。 8) 關於其餘 的一些方法的實現都很簡單易懂,讀者可參照API文檔和源代碼,一看便知,這裏就再也不多說。