Java 如何硬拷贝列表<;列表<&燃气轮机&燃气轮机;在爪哇? publicstaticvoidmain(字符串[]args){ List ll1=新的ArrayList(); ll1.添加(新阵列列表(1)); System.out.println(ll1.get(0)); //硬拷贝不起作用 列表ll2=新的阵列列表(ll1); System.out.println(ll2.get(0)=ll1.get(0)); }

Java 如何硬拷贝列表<;列表<&燃气轮机&燃气轮机;在爪哇? publicstaticvoidmain(字符串[]args){ List ll1=新的ArrayList(); ll1.添加(新阵列列表(1)); System.out.println(ll1.get(0)); //硬拷贝不起作用 列表ll2=新的阵列列表(ll1); System.out.println(ll2.get(0)=ll1.get(0)); },java,list,copy,Java,List,Copy,除了使用for循环对每个内部列表进行硬拷贝外,我们还有其他方法进行硬拷贝吗。 你能解释一下列表ll2=newarraylist(ll1)是如何产生的吗工作及其失败原因?您还需要复制内部列表: public static void main(String[] args) { List<List<Integer>> ll1 = new ArrayList<List<Integer>>(); ll1.add(new Array

除了使用for循环对每个内部列表进行硬拷贝外,我们还有其他方法进行硬拷贝吗。
你能解释一下列表ll2=newarraylist(ll1)是如何产生的吗工作及其失败原因?

您还需要复制内部列表:

    public static void main(String[] args) {
     List<List<Integer>> ll1 = new ArrayList<List<Integer>>();
     ll1.add(new ArrayList<Integer>(1));
     System.out.println(ll1.get(0));
     //hard copy don't work
     List<List<Integer>> ll2 = new ArrayList<List<Integer>>(ll1);
     System.out.println(ll2.get(0) == ll1.get(0));
}
私有列表复制(列表复制){
List copy=new ArrayList(toCopy.size());
用于(列表内部:toCopy){
copy.add(新ArrayList(内部));
}
返回副本;
}
private List<List<Integer>> copy(List<List<Integer>> toCopy) {
    List<List<Integer>> copy = new ArrayList<>(toCopy.size());
    for(List<Integer> inner : toCopy) {
        copy.add(new ArrayList<Integer>(inner));
    }
    return copy;
}