LinkedList implementationCollections.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機制:
待續中...