Java中的CopyOnWriteArrayList類
類宣告
public class CopyOnWriteArrayList extends Object implements List, RandomAccess, Cloneable, Serializable
CopyOnWriteArrayList是ArrayList的執行緒安全變體,其中可以更改ArrayList的操作(新增、更新、設定方法)會建立底層陣列的克隆。
CopyOnWriteArrayList應該在基於執行緒的環境中使用,在該環境中讀取操作非常頻繁,而更新操作很少。
CopyOnWriteArrayList的迭代器永遠不會丟擲ConcurrentModificationException異常。
對CopyOnWriteArrayList的任何型別的修改都不會在迭代器建立後反映在迭代過程中。
在迭代過程中不支援remove、set和add之類的列表修改方法。此方法將丟擲UnsupportedOperationException異常。
可以將null新增到列表中。
CopyOnWriteArrayList方法
以下是CopyOnWriteArrayList類中一些重要方法的列表。
| 序號 | 方法和描述 |
|---|---|
| 1 | void add(int index, Object element) 在此列表的指定位置index插入指定的元素。如果指定的索引超出範圍 (index ≥ size()),則丟擲IndexOutOfBoundsException異常。 |
| 2 | boolean add(Object o) 將指定的元素追加到此列表的末尾。 |
| 3 | boolean addAll(Collection c) 按指定集合的迭代器返回的順序,將指定集合中的所有元素追加到此列表的末尾。如果指定的集合為null,則丟擲NullPointerException異常。 |
| 4 | boolean addAll(int index, Collection c) 從指定位置開始,將指定集合中的所有元素插入到此列表中。如果指定的集合為null,則丟擲NullPointerException異常。 |
| 5 | void clear() 從此列表中刪除所有元素。 |
| 6 | Object clone() 返回此ArrayList的淺複製。 |
| 7 | boolean contains(Object o) 如果此列表包含指定的元素,則返回true。更正式地說,當且僅當此列表至少包含一個元素e,使得(o==null ? e==null : o.equals(e))時,返回true。 |
| 8 | Object get(int index) 返回此列表中指定位置的元素。如果指定的索引超出範圍 (index ≥ size()),則丟擲IndexOutOfBoundsException異常。 |
| 9 | int indexOf(Object o) 返回此列表中指定元素的第一次出現的索引,如果列表不包含此元素,則返回-1。 |
| 10 | int lastIndexOf(Object o) 返回此列表中指定元素的最後一次出現的索引,如果列表不包含此元素,則返回-1。 |
| 11 | Object remove(int index) 從此列表中刪除指定位置的元素。如果索引超出範圍 (index ≥ size()),則丟擲IndexOutOfBoundsException異常。 |
| 12 | Object set(int index, Object element) 將此列表中指定位置的元素替換為指定的元素。如果指定的索引超出範圍 (index ≥ size()),則丟擲IndexOutOfBoundsException異常。 |
| 13 | int size() 返回此列表中元素的數量。 |
| 14 | Object[] toArray() 返回一個包含此列表中所有元素的陣列,順序正確。如果指定的陣列為null,則丟擲NullPointerException異常。 |
示例
以下程式演示了ArrayList支援的幾種方法:
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class Tester {
public static void main(String args[]) {
// create an array list
CopyOnWriteArrayList al = new CopyOnWriteArrayList();
System.out.println("Initial size of al: " + al.size());
// add elements to the array list
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " + al.size());
// display the array list
System.out.println("Contents of al: " + al);
// Remove elements from the array list
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " + al.size());
System.out.println("Contents of al: " + al);
try {
Iterator iterator = al.iterator();
while(iterator.hasNext()) {
iterator.remove();
}
}catch(UnsupportedOperationException e) {
System.out.println("Method not supported:");
}
System.out.println("Size of al: " + al.size());
}
}這將產生以下結果:
輸出
Initial size of al: 0 Size of al after additions: 7 Contents of al: [C, A2, A, E, B, D, F] Size of al after deletions: 5 Contents of al: [C, A2, E, B, D] Method not supported: Size of al: 5
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP