工作中經常聽到別人講“容器”,各種各樣的容器,話說到底什么是容器,通俗的講“容器就是用來裝東西的器皿,比如:水桶就是用來盛水的,水桶就是一個容器。”
ok,在我們寫程序的時候常常要對大量的對象進行管理,比如查詢,遍歷,修改等。jdk為我們提供的容器位於java.util包,也是我們平時用的最多的包之一。
但是為什么不用數組(其實也不是不用,只是不直接用)呢,因為數組的長度需要提前確定,而且不能改變大小,用起來手腳受限嘛。
下面步入正題,首先我們想,一個對象管理容器需要哪些功能?增加,刪除,修改,查詢(crud對不對?)還有呢?遍歷,容量,是否包含某個元素。。。
功能是有了,如果讓你自己實現一個這樣的容器該怎么實現呢?
我們看看ArrayList是怎么實現這些功能的。
1.定義
首先先來看下頂級接口Collection的定義,
1 public interface Collection<E> extends Iterable<E> { 2 int size(); 3 boolean isEmpty(); 4 boolean contains(Object o); 5 Iterator<E> iterator(); 6 Object[] toArray(); 7 <T> T[] toArray(T[] a); 8 boolean add(E e); 9 boolean remove(Object o); 10 boolean containsAll(Collection<?> c); 11 boolean addAll(Collection<? extends E> c); 12 boolean removeAll(Collection<?> c); 13 boolean retainAll(Collection<?> c); 14 void clear(); 15 boolean equals(Object o); 16 int hashCode(); 17 }
然后是接口List的定義,
1 public interface List<E> extends Collection<E> { 2 int size(); 3 boolean isEmpty(); 4 boolean contains(Object o); 5 Iterator<E> iterator(); 6 Object[] toArray(); 7 <T> T[] toArray(T[] a); 8 boolean add(E e); 9 boolean remove(Object o); 10 boolean containsAll(Collection<?> c); 11 boolean addAll(Collection<? extends E> c); 12 boolean addAll( int index, Collection<? extends E> c); 13 boolean removeAll(Collection<?> c); 14 boolean retainAll(Collection<?> c); 15 void clear(); 16 boolean equals(Object o); 17 int hashCode(); 18 E get( int index); 19 E set( int index, E element); 20 void add( int index, E element); 21 E remove( int index); 22 int indexOf(Object o); 23 int lastIndexOf(Object o); 24 ListIterator<E> listIterator(); 25 ListIterator<E> listIterator( int index); 26 List<E> subList( int fromIndex, int toIndex); 27 }
再看下ArrayList的定義,
1 public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
可以看出ArrayList繼承
AbstractList(這是一個抽象類,對一些基礎的list操作進行封裝),實現
List,RandomAccess,Cloneable,Serializable幾個接口,RandomAccess是一個標記接口,用來表明其支持快速隨機訪問。
2.底層存儲
顧名思義哈,ArrayList就是用數組實現的List容器,既然是用數組實現,當然底層用數組來保存數據啦。。。
1 private transient Object[] elementData; 2 private int size;
可以看到用一個Object數組來存儲數據,用一個int值來計數,記錄當前容器的數據大小。
另外,細心的人會發現
elementData數組是使用
transient修飾的,關於
transient關鍵字的作用簡單說就是java自帶默認機制進行序列化的時候,被其修飾的屬性不需要維持。會不會產生一點疑問?
elementData不需要維持,那么怎么進行反序列化,又怎么保證序列化和反序列化數據的正確性?難道不需要存儲?用大腿想一下那當然是不可以的嘛,既然需要存儲,它是怎么實現的呢?注意上面紅色加粗的地方,默認序列化機制,嗯哼想明白了ArrayList一定是使用了自定義的序列化方式,到底是不是這樣的呢?看下面兩個方法:
1 /** 2 * Save the state of the <tt>ArrayList</tt> instance to a stream (that 3 * is, serialize it). 4 * 5 * @serialData The length of the array backing the <tt>ArrayList </tt> 6 * instance is emitted (int), followed by all of its elements 7 * (each an <tt>Object</tt> ) in the proper order. 8 */ 9 private void writeObject(java.io.ObjectOutputStream s) 10 throws java.io.IOException{ 11 // Write out element count, and any hidden stuff 12 int expectedModCount = modCount ; 13 s.defaultWriteObject(); 14 15 // Write out array length 16 s.writeInt( elementData.length ); 17 18 // Write out all elements in the proper order. 19 for (int i=0; i<size; i++) 20 s.writeObject( elementData[i]); 21 22 if (modCount != expectedModCount) { 23 throw new ConcurrentModificationException(); 24 } 25 26 } 27 28 /** 29 * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is, 30 * deserialize it). 31 */ 32 private void readObject(java.io.ObjectInputStream s) 33 throws java.io.IOException, ClassNotFoundException { 34 // Read in size, and any hidden stuff 35 s.defaultReadObject(); 36 37 // Read in array length and allocate array 38 int arrayLength = s.readInt(); 39 Object[] a = elementData = new Object[arrayLength]; 40 41 // Read in all elements in the proper order. 42 for (int i=0; i<size; i++) 43 a[i] = s.readObject(); 44 }
英語注釋很詳細,也很容易讀懂,就不進行翻譯了。那么想一下為什么要這樣設計呢,豈不是很麻煩。下面簡單進行解釋下:
elementData
是一個數據存儲數組,而數組是定長的,它會初始化一個容量,等容量不足時再擴充容量(擴容方式為數據拷貝,后面會詳細解釋),再通俗一點說就是比如
elementData 的長度是10,而里面只保存了3個對象,那么數組中其余的7個元素(null)是沒有意義的,所以也就不需要保存,以節省序列化后的內存容量,好了到這里就明白了這樣設計的初衷和好處,順便好像也明白了長度單獨用一個int變量保存,而不是直接使用
elementData.length的原因。
3.構造方法
1 /** 2 * 構造一個具有指定容量的list 3 */ 4 public ArrayList( int initialCapacity) { 5 super(); 6 if (initialCapacity < 0) 7 throw new IllegalArgumentException( "Illegal Capacity: " + 8 initialCapacity); 9 this.elementData = new Object[initialCapacity]; 10 } 11 12 /** 13 * 構造一個初始容量為10的list 14 */ 15 public ArrayList() { 16 this(10); 17 } 18 19 /** 20 * 構造一個包含指定元素的list,這些元素的是按照Collection的迭代器返回的順序排列的 21 */ 22 public ArrayList(Collection<? extends E> c) { 23 elementData = c.toArray(); 24 size = elementData .length; 25 // c.toArray might (incorrectly) not return Object[] (see 6260652) 26 if (elementData .getClass() != Object[].class) 27 elementData = Arrays.copyOf( elementData, size , Object[].class); 28 }
構造方法看完了,想一下指定容量的構造方法的意義,既然默認為10就可以那么為什么還要提供一個可以指定容量大小的構造方法呢?在這里說好像有點太早,那就賣個關子,下面再說。。。
4.增加
1 /** 2 * 添加一個元素 3 */ 4 public boolean add(E e) { 5 // 進行擴容檢查 6 ensureCapacity( size + 1); // Increments modCount 7 // 將e增加至list的數據尾部,容量+1 8 elementData[size ++] = e; 9 return true; 10 } 11 12 /** 13 * 在指定位置添加一個元素 14 */ 15 public void add(int index, E element) { 16 // 判斷索引是否越界,這里會拋出多么熟悉的異常。。。 17 if (index > size || index < 0) 18 throw new IndexOutOfBoundsException( 19 "Index: "+index+", Size: " +size); 20 21 // 進行擴容檢查 22 ensureCapacity( size+1); // Increments modCount 23 // 對數組進行復制處理,目的就是空出index的位置插入element,並將index后的元素位移一個位置 24 System. arraycopy(elementData, index, elementData, index + 1, 25 size - index); 26 // 將指定的index位置賦值為element 27 elementData[index] = element; 28 // list容量+1 29 size++; 30 } 31 /** 32 * 增加一個集合元素 33 */ 34 public boolean addAll(Collection<? extends E> c) { 35 //將c轉換為數組 36 Object[] a = c.toArray(); 37 int numNew = a.length ; 38 //擴容檢查 39 ensureCapacity( size + numNew); // Increments modCount 40 //將c添加至list的數據尾部 41 System. arraycopy(a, 0, elementData, size, numNew); 42 //更新當前容器大小 43 size += numNew; 44 return numNew != 0; 45 } 46 /** 47 * 在指定位置,增加一個集合元素 48 */ 49 public boolean addAll(int index, Collection<? extends E> c) { 50 if (index > size || index < 0) 51 throw new IndexOutOfBoundsException( 52 "Index: " + index + ", Size: " + size); 53 54 Object[] a = c.toArray(); 55 int numNew = a.length ; 56 ensureCapacity( size + numNew); // Increments modCount 57 58 // 計算需要移動的長度(index之后的元素個數) 59 int numMoved = size - index; 60 // 數組復制,空出第index到index+numNum的位置,即將數組index后的元素向右移動numNum個位置 61 if (numMoved > 0) 62 System. arraycopy(elementData, index, elementData, index + numNew, 63 numMoved); 64 65 // 將要插入的集合元素復制到數組空出的位置中 66 System. arraycopy(a, 0, elementData, index, numNew); 67 size += numNew; 68 return numNew != 0; 69 } 70 71 /** 72 * 數組容量檢查,不夠時則進行擴容 73 */ 74 public void ensureCapacity( int minCapacity) { 75 modCount++; 76 // 當前數組的長度 77 int oldCapacity = elementData .length; 78 // 最小需要的容量大於當前數組的長度則進行擴容 79 if (minCapacity > oldCapacity) { 80 Object oldData[] = elementData; 81 // 新擴容的數組長度為舊容量的1.5倍+1 82 int newCapacity = (oldCapacity * 3)/2 + 1; 83 // 如果新擴容的數組長度還是比最小需要的容量小,則以最小需要的容量為長度進行擴容 84 if (newCapacity < minCapacity) 85 newCapacity = minCapacity; 86 // minCapacity is usually close to size, so this is a win: 87 // 進行數據拷貝,Arrays.copyOf底層實現是System.arrayCopy() 88 elementData = Arrays.copyOf( elementData, newCapacity); 89 } 90 }
5.刪除
1 /** 2 * 根據索引位置刪除元素 3 */ 4 public E remove( int index) { 5 // 數組越界檢查 6 RangeCheck(index); 7 8 modCount++; 9 // 取出要刪除位置的元素,供返回使用 10 E oldValue = (E) elementData[index]; 11 // 計算數組要復制的數量 12 int numMoved = size - index - 1; 13 // 數組復制,就是將index之后的元素往前移動一個位置 14 if (numMoved > 0) 15 System. arraycopy(elementData, index+1, elementData, index, 16 numMoved); 17 // 將數組最后一個元素置空(因為刪除了一個元素,然后index后面的元素都向前移動了,所以最后一個就沒用了),好讓gc盡快回收 18 // 不要忘了size減一 19 elementData[--size ] = null; // Let gc do its work 20 21 return oldValue; 22 } 23 24 /** 25 * 根據元素內容刪除,只刪除匹配的第一個 26 */ 27 public boolean remove(Object o) { 28 // 對要刪除的元素進行null判斷 29 // 對數據元素進行遍歷查找,知道找到第一個要刪除的元素,刪除后進行返回,如果要刪除的元素正好是最后一個那就慘了,時間復雜度可達O(n) 。。。 30 if (o == null) { 31 for (int index = 0; index < size; index++) 32 // null值要用==比較 33 if (elementData [index] == null) { 34 fastRemove(index); 35 return true; 36 } 37 } else { 38 for (int index = 0; index < size; index++) 39 // 非null當然是用equals比較了 40 if (o.equals(elementData [index])) { 41 fastRemove(index); 42 return true; 43 } 44 } 45 return false; 46 } 47 48 /* 49 * Private remove method that skips bounds checking and does not 50 * return the value removed. 51 */ 52 private void fastRemove(int index) { 53 modCount++; 54 // 原理和之前的add一樣,還是進行數組復制,將index后的元素向前移動一個位置,不細解釋了, 55 int numMoved = size - index - 1; 56 if (numMoved > 0) 57 System. arraycopy(elementData, index+1, elementData, index, 58 numMoved); 59 elementData[--size ] = null; // Let gc do its work 60 } 61 62 /** 63 * 數組越界檢查 64 */ 65 private void RangeCheck(int index) { 66 if (index >= size ) 67 throw new IndexOutOfBoundsException( 68 "Index: "+index+", Size: " +size); 69 }
PS:看到了這個方法,便可jdk源碼有些地方寫的也不是那么精巧,比如這里remove時將數組越界檢查封裝成了一個單獨方法,可是往前翻一下add方法中的數組越界就沒有進行封裝,需要檢查的時候都是寫一遍一樣的代碼,why啊。。。
增加和刪除方法到這里就解釋完了,代碼是很簡單,主要需要特別關心的就兩個地方:1.數組擴容,2.數組復制,這兩個操作都是極費效率的,最慘的情況下(添加到list第一個位置,刪除list最后一個元素或刪除list第一個索引位置的元素)時間復雜度可達O(n)。
還記得上面那個坑嗎(為什么提供一個可以指定容量大小的構造方法 )?看到這里是不是有點明白了呢,簡單解釋下:如果數組初試容量過小,假設默認的10個大小,而我們使用ArrayList的主要操作時增加元素,不斷的增加,一直增加,不停的增加,會出現上面后果?那就是數組容量不斷的受挑釁,數組需要不斷的進行擴容,擴容的過程就是數組拷貝System.arraycopy的過程,每一次擴容就會開辟一塊新的內存空間和數據的復制移動,這樣勢必對性能造成影響。那么在這種以寫為主(寫會擴容,刪不會縮容)場景下,提前預知性的設置一個大容量,便可減少擴容的次數,提高了性能。
圖1.
圖2.
上面兩張圖分別是數組擴容和數組復制的過程,需要注意的是,數組擴容伴隨着開辟新建的內存空間以創建新數組然后進行數據復制,而數組復制不需要開辟新內存空間,只需將數據進行復制。
上面講增加元素可能會進行擴容,而刪除元素卻不會進行縮容,如果在已刪除為主的場景下使用list,一直不停的刪除而很少進行增加,那么會出現什么情況?再或者數組進行一次大擴容后,我們后續只使用了幾個空間,會出現上面情況?當然是空間浪費啦啦啦,怎么辦呢?
1 /** 2 * 將底層數組的容量調整為當前實際元素的大小,來釋放空間。 3 */ 4 public void trimToSize() { 5 modCount++; 6 // 當前數組的容量 7 int oldCapacity = elementData .length; 8 // 如果當前實際元素大小 小於 當前數組的容量,則進行縮容 9 if (size < oldCapacity) { 10 elementData = Arrays.copyOf( elementData, size ); 11 } 12
6.更新
1 /** 2 * 將指定位置的元素更新為新元素 3 */ 4 public E set( int index, E element) { 5 // 數組越界檢查 6 RangeCheck(index); 7 8 // 取出要更新位置的元素,供返回使用 9 E oldValue = (E) elementData[index]; 10 // 將該位置賦值為行的元素 11 elementData[index] = element; 12 // 返回舊元素 13 return oldValue; 14 }
7.查找
1 /** 2 * 查找指定位置上的元素 3 */ 4 public E get( int index) { 5 RangeCheck(index); 6 7 return (E) elementData [index]; 8 }
由於ArrayList使用數組實現,更新和查找直接基於下標操作,變得十分簡單。
8.是否包含
1 /** 2 * Returns <tt>true</tt> if this list contains the specified element. 3 * More formally, returns <tt>true</tt> if and only if this list contains 4 * at least one element <tt>e</tt> such that 5 * <tt>(o==null ? e==null : o.equals(e))</tt>. 6 * 7 * @param o element whose presence in this list is to be tested 8 * @return <tt> true</tt> if this list contains the specified element 9 */ 10 public boolean contains(Object o) { 11 return indexOf(o) >= 0; 12 } 13 14 /** 15 * Returns the index of the first occurrence of the specified element 16 * in this list, or -1 if this list does not contain the element. 17 * More formally, returns the lowest index <tt>i</tt> such that 18 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, 19 * or -1 if there is no such index. 20 */ 21 public int indexOf(Object o) { 22 if (o == null) { 23 for (int i = 0; i < size; i++) 24 if (elementData [i]==null) 25 return i; 26 } else { 27 for (int i = 0; i < size; i++) 28 if (o.equals(elementData [i])) 29 return i; 30 } 31 return -1; 32 } 33 34 /** 35 * Returns the index of the last occurrence of the specified element 36 * in this list, or -1 if this list does not contain the element. 37 * More formally, returns the highest index <tt>i</tt> such that 38 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, 39 * or -1 if there is no such index. 40 */ 41 public int lastIndexOf(Object o) { 42 if (o == null) { 43 for (int i = size-1; i >= 0; i--) 44 if (elementData [i]==null) 45 return i; 46 } else { 47 for (int i = size-1; i >= 0; i--) 48 if (o.equals(elementData [i])) 49 return i; 50 } 51 return -1; 52 }
contains主要是檢查indexOf,也就是元素在list中出現的索引位置也就是數組下標,再看
indexOf和lastIndexOf代碼是不是很熟悉,沒錯,和public boolean remove(Object o) 的代碼一樣,都是元素null判斷,都是循環比較,不多說了。。。但是要知道,最差的情況(要找的元素是最后一個)也是很慘的。。。
9.容量判斷
1 /** 2 * Returns the number of elements in this list. 3 * 4 * @return the number of elements in this list 5 */ 6 public int size() { 7 return size ; 8 } 9 10 /** 11 * Returns <tt>true</tt> if this list contains no elements. 12 * 13 * @return <tt> true</tt> if this list contains no elements 14 */ 15 public boolean isEmpty() { 16 return size == 0; 17 }
由於使用了size進行計數,發現list大小獲取和判斷真的好容易。。。
好了,至此ArrayList的分析和注釋就基本完成了。什么還差些什么?對,
modCount 是干什么的,怎么到處都在操作這個變量,還有遍歷呢,為啥不講?由於iterator遍歷相對比較復雜,而且iterator 是GoF經典設計模式比較重要的一個,之后會對iterator單獨分析,這里就不啰嗦了。。。
ArrayList,完!
