要返回 CopyOnWriteArrayList 的浅拷贝,请使用 clone ()方法。
语法如下:
public Object clone()
要使用CopyOnWriteArrayList类,您需要导入以下程序包:
import java.util.concurrent.CopyOnWriteArrayList;
以下是clone()
在Java中实现CopyOnWriteArrayList类方法的示例:
import java.util.concurrent.CopyOnWriteArrayList; public class Demo { public static void main(String[] args) { CopyOnWriteArrayList<Integer> arrList = new CopyOnWriteArrayList<Integer>(); arrList.add(30); arrList.add(90); arrList.add(80); arrList.add(70); arrList.add(90); arrList.add(100); arrList.add(120); System.out.println("CopyOnWriteArrayList = " + arrList); System.out.println("Cloned CopyOnWriteArrayList = " + arrList.clone()); } }
输出结果
CopyOnWriteArrayList = [30, 90, 80, 70, 90, 100, 120] Cloned CopyOnWriteArrayList = [30, 90, 80, 70, 90, 100, 120]