java:List的深拷贝

最近从C++转JAVA很多不适应的地方

List的拷贝就是其中之一

//假如申请一个List
List<Integer> temp = new ArrayList<>();
//另申请一个直接相等, List
<Integer> t1 = temp;

temp.add(1);
t1.forEach(System.out::println);//输出1

可见是浅拷贝

如需深拷贝,可用

1.

List<Integer> t1 = new ArrayList<>(temp);  //最简洁

2.

List<Integer> t1 = new ArrayList<>();
Collections.addAll(t1, new Integer[temp.size()]; //0数组
Collections.copy(t1,temp); //拷贝,实则覆盖替换,不是追加

3.

可以把temp变为Integer[]数组

再通过Collections.addAll赋值

诸位正值青春年少,一定恣情放纵,贪恋香艳梅施之情,喜欢风流雅韵之事,洒脱木拘。然而诸位可知,草上露一碰即落,竹上霜一触即溶,此种风情难于长久。
原文地址:https://www.cnblogs.com/shilipojianshen/p/12773949.html