1
    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?

4 Answers 4

5

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 .

Sign up to request clarification or add additional context in comments.

Comments

2

Output is self explanatory.

Misconceptions

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.

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

It will add the value 3 at the index 1 in the temp list .

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]]

Comments

0

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.

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]]

Comments

0

The problem is that you do not copy the values of temp into result but you are just giving a reference to 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));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.