IMLC.ME
Constructor
List<Integer> copy = new ArrayList<>(list);
List#allAll
List<Integer> copy = new ArrayList<>();
copy.addAll(list);
List#copyOf (Java 10)
List<T> copy = List.copyOf(list);
注意事项

由于新的列表只是保存列表元素的引用,在一个列表中修改了某元素的内部属性,也会影响到另外一个列表。

这在实际编码中很容易出问题。

一般我们遵循如下的最佳实践:

  1. 不要把复制后的数组暴露给外部代码
  2. 尽量使用不可变的元素
  3. 复制列表的同时也复制元素