【學習篇】java-集合類-ArrayList,java--arraylist
1.ArrayList類
API文檔地址:http://docs.oracle.com/javase/7/docs/api/
翻譯過來大致意思(英文非常一般,有錯誤請大家幫忙指出並糾正,謝謝):通過可變數組實現介面 List 介面。實現所有可選列表的操作,並且運行所有元素都為null. 除了實現介面中的方法外,還提供了一個方法運算元組大小來儲存列表中的數組大小。(該類相當於個體,是不同步的,也就是線程不安全)
size ,isEmpty,get.set.iterator an listIterator 這些操作運行在固定的時間中,add 操作運行在分階段的固定時間,也就是添加n個元素則需要O(n)的時間。所有其他動作已耗用時間都是單線程(粗略的講)。The constant factor is low compared to that for the LinkedList implementation
所有ArrayList都有一個容量。容量用來存放數組元素列表。容量始終和數組列表一樣大小。元素添加到ArrayList中後,容量會自定增長。The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost。
在添加大量元素前,應用程式可以說使用ensureCapacity方法操作增加執行個體容量,這樣可以減少遞增式再分配的數量。
注意,次實現是不同步的。如果多個線程同時訪問同一個ArrayList執行個體,如果其中至少有一個線程修改了列表,那麼必須要同外部同步。(修改是指增加 或者添加一個或多個元素,或者調整支援陣列;僅僅修改一個元素的值不是一個結構修改)通常的是通過封裝的對象方法來完成。如果不存在這樣的對象方法,他應 該使用
Collections.synchronizedList 方法。最好的做法是在建立時,防止其他非同步訪問列表。
List list = Collections.synchronizedList(new ArrayList(...)); The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw aConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.。 Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs 這個類是java集合類中的一員 從官方介紹上,我們可以知道大約幾點情況: 1. ArrayList 源碼中是通過數組實現(實際上看類名 array 就知道數組了) 2.添加新元素時,效率會有影響。訪問查詢迭代操作會比較快速 3.ArrayList有一個容量值,通過ensureCapacity方法設定這個容量值,來協助arrayList最佳化(因為java中數組是固定值,如果arrayList存放的值比較大,我們應該提前告訴arrayList需要多的容量的數組) 4.ArrayList不是現場安全的,可以用 Collections.synchronizedList 方法協助同步。 5.fail-fast 機制 2.看看ArrayList類 聲明了4個屬性如下: prviate static final int DEFAULT_CATACITY = 10 ; //變數名翻譯過來就是 預設容量 private static final Object[] EMPTY_ELEMENTDATA={}; // 就是一個空的數組 private transient Object[] elementData; //有個數組,這個就是ArrayList儲存物件的數組 private int size; //元素的個數 從4個屬性上,可以看到 ArrayList 底層是通過數組結構在存放的 elementData 問: 1. 聲明屬性關鍵字 transient 有什麼用? 2. 既然有了數組 elementData, 擷取長度用 .length 就可以了,為什麼要用 size ? 3.構造方法: 不多就3個如下: 1. 自己給數組長度
public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; } 2. 預設一個空數組
public ArrayList() { super(); this.elementData = EMPTY_ELEMENTDATA; } 3.給一個Collection到數組中,可以看到,ArrayList通過複製的方式,把Collection複製到elementData中
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); }
4.操作方法: 操作方法有: add(E e) ,add(int index,E element), addAll(Collection<? extends E> c),addAll(int index,Collection<? extends E> c) 等等,大家可以看看源碼由於主要說明ArrayList是在運算元組,所以這些方法中,只拿其中一個說明下。
1. 添加一個元素方法
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } 從方法體中,可以看到分2步操作,1.聲明一個數組長度,2.給數組下標賦值。 5. ensureCapacity 方法
public void ensureCapacity(int minCapacity) { // 判斷是否一個 空數組,如果是返回 DEFAULT_CAPACITY ,主要就是告訴你,別犯賤在ArrayList聲明還沒使用的情況下就設定一個小於預設值的容量進來。 int minExpand = (elementData != EMPTY_ELEMENTDATA) // any size if real element table ? 0 // larger than default for empty table. It's already supposed to be // at default size. : DEFAULT_CAPACITY; if (minCapacity > minExpand) { ensureExplicitCapacity(minCapacity); } } 這個方法在ArrayList的容量中已經有元素的情況下可以使用,一般都是在不斷有大量 Collection 進來的時候用,因為提前告訴系統要一個很大的數組裝元素,不要不斷的建立新數組,複製消耗效能。 6.
trimToSize 方法:
public void trimToSize() { modCount++; if (size < elementData.length) { elementData = Arrays.copyOf(elementData, size); } } ArrayList還給我們提供了將底層數組的容量調整為當前列表儲存的實際元素的大小的功能。它可以通過trimToSize方法來實現
7.Fail-Fast機制:
待續中...