Java 中的 CopyOnWriteArrayList 類


類宣告

public class CopyOnWriteArrayList
   extends Object
implements List, RandomAccess, Cloneable, Serializable
  • CopyOnWriteArrayList 是 ArrayList 的執行緒安全變體,其中可以更改 ArrayList 的操作(新增、更新、設定方法)會建立底層陣列的克隆。

  • CopyOnWriteArrayList 應該用於基於執行緒的環境中,在這些環境中讀取操作非常頻繁,而更新操作很少。

  • CopyOnWriteArrayList 的迭代器永遠不會丟擲 ConcurrentModificationException 異常。

  • 對 CopyOnWriteArrayList 的任何型別的修改在迭代期間都不會反映出來,因為迭代器已建立。

  • 在迭代中不支援 List 修改方法,例如 remove、set 和 add。此方法將丟擲 UnsupportedOperationException 異常。

  • 可以將 null 新增到列表中。

CopyOnWriteArrayList 方法

以下是 CopyOnWriteArrayList 類中可用的一些重要方法。

序號
方法及描述
1
void add(int index, Object element)

在此列表的指定位置 index 插入指定的元素。如果指定的 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 超出範圍 (index size()),則丟擲 IndexOutOfBoundsException 異常。

9
int indexOf(Object o)

返回此列表中指定元素第一次出現的索引,如果此列表不包含此元素,則返回 -1。

10
int lastIndexOf(Object o)

返回此列表中指定元素最後一次出現的索引,如果此列表不包含此元素,則返回 -1。

11
Object remove(int index)

從此列表中刪除指定位置的元素。如果 index 超出範圍 (index size()),則丟擲 IndexOutOfBoundsException 異常。

12
Object set(int index, Object element)

將此列表中指定位置的元素替換為指定的元素。如果指定的 index 超出範圍 (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

更新時間: 2020年6月19日

4K+ 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.