Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
列表<;列表<;整数>&燃气轮机;ArrayList初始化不适用于新运算符Java_Java_For Loop_Arraylist_Initialization_Variable Assignment - Fatal编程技术网

列表<;列表<;整数>&燃气轮机;ArrayList初始化不适用于新运算符Java

列表<;列表<;整数>&燃气轮机;ArrayList初始化不适用于新运算符Java,java,for-loop,arraylist,initialization,variable-assignment,Java,For Loop,Arraylist,Initialization,Variable Assignment,我在这个问题上挣扎了两天。这个问题来自我正在做的某个问题。基本上,当我使用 List<List<Integer>> temp = new ArrayList<>(result); List temp=new ArrayList(结果); 要创建结果的新ArrayList副本,当我尝试在高级for循环中更改temp时,结果将更改。比如说, List<List<Integer>> result = new ArrayList<&g

我在这个问题上挣扎了两天。这个问题来自我正在做的某个问题。基本上,当我使用

List<List<Integer>> temp = new ArrayList<>(result);
List temp=new ArrayList(结果);
要创建结果的新ArrayList副本,当我尝试在高级for循环中更改temp时,结果将更改。比如说,

List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>());
List<List<Integer>> temp = new ArrayList<>(result);
int j = 0;
for (List<Integer> list: temp) {
    list.add(x[j]);
    j ++;
}
List result=new ArrayList();
添加(新的ArrayList());
列表温度=新阵列列表(结果);
int j=0;
用于(列表:临时){
列表。添加(x[j]);
j++;
}
我没有对循环中的结果做任何更改,但结果以[[1]]结束,这与temp相同

为什么会这样?非常感谢

更新:谢谢大家回答我的问题。我知道原因是肤浅的抄袭。然而,我仍然遇到类似的问题。在我尝试更改以下代码中的临时值时,结果会更新:

List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>());
List<List<Integer>> temp = new ArrayList<>();
for (List<Integer> list: result) {
    list.add(10000);
    temp.add(new ArrayList(list));
}
List result=new ArrayList();
添加(新的ArrayList());
List temp=new ArrayList();
对于(列表:结果){
增加(10000);
临时添加(新阵列列表(列表));
}

我不知道为什么结果是[[10000]]以及温度。像temp.add(newarraylist(list))这样的add方法有什么问题吗?

这并不奇怪,因为您只有一个列表可以迭代。如果您添加第二个列表,您将看到第二个数字

int[] x = new int[]{1,2,3,4,5}; 

    List<List<Integer>> result = new ArrayList<>();
    result.add(new ArrayList());
result.add(new ArrayList());
    List<List<Integer>> temp = new ArrayList<>(result);
    for (Integer xx: x) {
        result.add(new ArrayList(xx));
    }
    System.out.println(result.toString());
temp=new ArrayList(result)
只复制
result
的浅层副本,即它复制“外部”列表,但不复制其元素

temp.get(0)==result.get(0)
-我不是说
等于
-它们是完全相同的实例


因此,添加到
temp.get(0)
的任何内容也将出现在
result.get(0)
中,因为
List temp=new ArrayList(result)语句仅复制顶级列表。新列表将包含原始
结果
中对原始项(也称为子列表)的引用

您可以使用深度副本修复此问题:

List<List<Integer>> temp = new ArrayList<>(); // empty list
for (List<Integer> sublist : result) {
    temp.add(new ArrayList<Integer>(result)); // copying a sublist and adding that
}
List temp=new ArrayList();//空列表
对于(列表子列表:结果){
temp.add(newarraylist(result));//复制子列表并添加该子列表
}

另外,x在[j]前面做什么?
新的ArrayList(result)
只做一个
result
的浅拷贝,即它复制“外部”列表,但不复制其元素。您好,先生,如果您有时间,您能进一步看看我新的类似问题吗?我在文章的最后更新了这个问题。嗨,先生,如果你有时间,你能进一步看看我新的类似问题吗?我在帖子的最后更新了这个问题。
List<List<Integer>> temp = new ArrayList<>(); // empty list
for (List<Integer> sublist : result) {
    temp.add(new ArrayList<Integer>(result)); // copying a sublist and adding that
}