简体   繁体   English

关于结果的java ArrayList

[英]java ArrayList about the result

    ArrayList<ArrayList<Integer>> result = new ArrayList<>();
    ArrayList<Integer> temp = new ArrayList<>();
    temp.add(1);
    temp.add(2);
    result.add(temp);
    temp.remove(temp.size() - 1);
    temp.add(1, 3);
    result.add(new ArrayList<>(temp));

The result is [[1, 3], [1, 3]], but I think it should be [[1, 2], [1, 3]], why? 结果是[[1,3],[1,3]],但我认为应该是[[1,2],[1,3]],为什么呢?

Follow comments 关注评论

temp.add(1);  /aaded 1 
temp.add(2);  / added 2
result.add(temp);
temp.remove(temp.size() - 1); // removed index 1 that i.e removed 2
temp.add(1, 3);   // added 3 at the index 1 again.  now it is 1,3 
result.add(new ArrayList<>(temp));  // And you are added a new array list again with temp 

And if I understand correctly, you misunderstood the logic at 如果我理解正确,您就会误解了

 temp.add(1, 3);

That means you are telling add the value 3 at the index 1 in the list temp . 这意味着您要告诉将值3添加到列表temp的索引1中。

Output is self explanatory. 输出不言自明。

Misconceptions 误解

1.) temp.remove(temp.size() - 1); 1.) temp.remove(temp.size() - 1);

This removes last element from temp list and since temp list is being referred inside result so it get referenced there as well. 这会从temp list删除最后一个元素,并且由于temp listresult内部被引用,因此它也会在其中被引用。

2.) temp.add(1, 3); 2.) temp.add(1, 3);

It will add the value 3 at the index 1 in the temp list . 它将在temp list的索引1处添加值3。

public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();

        ArrayList<Integer> temp = new ArrayList<>();
        temp.add(1);
        temp.add(2);
        System.out.println("Temp is : " + temp);

        result.add(temp);
        System.out.println("Result is : " + result);

        temp.remove(temp.size() - 1);
        System.out.println("Temp is : " + result);
        System.out.println("Result is : " + result);

        temp.add(1, 3);
        System.out.println("Temp is : " + temp);

        result.add(new ArrayList<>(temp));
        System.out.println("Result is : " + result);
    }

output 输出

Temp is : [1, 2]
Result is : [[1, 2]]
Temp is : [[1]]
Result is : [[1]]
Temp is : [1, 3]
Result is : [[1, 3], [1, 3]]

If you update temp directly then all the references pointing to that list is also updated (in your case result list 1st index), that's why you are getting [[1, 3], [1, 3]] output. 如果直接更新temp ,则指向该list所有引用也会被更新(在您的情况下, result列表为第一个索引),这就是为什么要获得[[1, 3], [1, 3]]输出的原因。

You can use below code. 您可以使用以下代码。

ArrayList<ArrayList<Integer>> result = new ArrayList<>();
ArrayList<Integer> temp = new ArrayList<>();
temp.add(1);  //added 1 
temp.add(2);  // added 2
result.add(temp);

// creating new object and populating it with the values of temp.
ArrayList<Integer> temp1 = new ArrayList<>(temp);

// or you can reinitialize temp with its previous values and then use it as you have done in your code.
// temp = new ArrayList<>(temp); 

temp1.remove(temp1.size() - 1);
temp1.add(1, 3); 
result.add(temp1);

System.out.println(result);

Output: 输出:

[[1, 2], [1, 3]]

The problem is that you do not copy the values of temp into result but you are just giving a reference to temp. 问题是您没有将temp的值复制到result中,而只是在引用temp。

By changing the reference you change the result. 通过更改参考,您可以更改结果。

To solve the problem, try this: 要解决此问题,请尝试以下操作:

public void test1() {
    ArrayList<ArrayList<Integer>> result = new ArrayList<>();
    ArrayList<Integer> temp = new ArrayList<>();
    ArrayList<Integer> temp2 = new ArrayList<>();
    temp.add(1);
    temp.add(2);
    result.add(new ArrayList<>(temp));
    temp.remove(temp.size() - 1);
    temp.add(1, 3);
    result.add(new ArrayList<>(temp));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM