本文將以如下幾個問題來探討ArrayList的源碼實現
1.ArrayList的大小是如何自動增長的
2.什麼狀況下你會使用ArrayList?何時你會選擇LinkedList?
3.如何複製某個ArrayList到另外一個ArrayList中去?寫出你的代碼?
4.在索引中ArrayList的增長或者刪除某個對象的運行過程?效率很低嗎?解釋一下爲何?
5.Interator在ArrayList的實現html
關於Java集合的小抄 關於ArrayList的描述: *以數組實現。節約空間,但數組有容量限制。超出限制時會增長50%容量,用System.arraycopy()複製到新的數組,所以最好能給出數組大小的預估值。默認第一次插入元素時建立大小爲10的數組。 按數組下標訪問元素--get(i)/set(i,e) 的性能很高,這是數組的基本優點。 直接在數組末尾加入元素--add(e)的性能也高,但若是按下標插入、刪除元素--add(i,e), remove(i), remove(e),則要用System.arraycopy()來移動部分受影響的元素,性能就變差了,這是基本劣勢。* java
一、ArrayList的大小是如何自動增長的數組
直接上代碼吧,每次add的時候都會判斷是否須要擴容,如下是擴容的主要方法 ```java private void ensureCapacityInternal(int minCapacity) { if (elementData == 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 static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; //左移1位,至關於除以2,就是容量提升50% int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; //最大的閥值MAX_ARRAY_SIZE 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); }
**二、什麼狀況下你會使用ArrayList?何時你會選擇LinkedList?** <p>咱們知道ArrayList和LinkedList的數據結構是不一樣的,ArrayList是以連續的數組進行存儲的,因此它的get是常數級別的,LinkedList是雙向鏈表存儲的。他的查詢最壞狀況是n。覺得ArrayList是數組存儲的,因此當你查找某一指定索引的數據時,它每次刪除和指定索引添加都要移動數組的位置,其內部的實現方式是數組複雜用到System.arraycope,是比較影響性能的,而雙向鏈表刪除和插入只要找到相應的節點位置,關聯下指針,因此性能會更好。 ```java public void add(int index, E element) { //判斷是否超出了索引 rangeCheckForAdd(index); //判斷是否須要擴容 ensureCapacityInternal(size + 1); // Increments modCount!! //講elementData複製到elementData,從index開始複製,從index+1開始粘貼,複製的長度是size-index System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; }
以及刪除方法app
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; }
三、如何複製某個ArrayList到另外一個ArrayList中去?寫出你的代碼?ide
下面就是把某個ArrayList複製到另外一個ArrayList中去的幾種技術: 性能
使用clone()方法,好比ArrayList newArray = oldArray.clone() ui
使用ArrayList構造方法,好比:ArrayList myObject = new ArrayList(myTempObject) this
其餘 ```java public Object clone() { try { ArrayList v = (ArrayList ) super.clone(); v.elementData = Arrays.copyOf(elementData, size); v.modCount = 0; return v; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(e); } } ``` ```java public ArrayList(Collection 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); } ``` 指針
綜上所述,最終的複製方式都是調用Arrays.copyOf,而Arrays.copyOf是調用System.arrayscopy ```java public static
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { @SuppressWarnings("unchecked") T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
**四、在索引中ArrayList的增長或者刪除某個對象的運行過程?效率很低嗎?** <p>這個問題同2,由於添加刪除某個索引的數據時,須要總體移動數組,因此效率比較低。 **五、Interator在ArrayList的實現** <p>由於這個實現的代碼比較簡單這裏就很少解釋了,特別說明下forEachRemaining,這個方法是jdk1.8加上的,支持lamdba表達式,主要是遍歷遊標後面的數據,看while循環i++ ```java 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(); //遊標的位置加1 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(); } }
參考