0
import java.util.*;

public class MyClass {

    public static void main(String args[]) {

        List<List<Integer>> l = new ArrayList<List<Integer>>();

        List<Integer> a = new ArrayList<Integer>();

        a.add(1);

        a.add(2);

        l.add(a);

        a.clear();

        a.add(4);

        a.add(5);

        l.add(a);

        System.out.println(l+" | ");
    }
}

output - [[4, 5], [4, 5]] |

I want it to display - [[1,2],[4,5]]

What is wrong with my code?

3 Answers 3

2

You clear the list after adding it to the list of lists. You need to create a new list instead:

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
        List<List<Integer>> l = new ArrayList<List<Integer>>();
        List<Integer> a = new ArrayList<Integer>();
        a.add(1);
        a.add(2);
        l.add(a);
        List<Integer> b = new ArrayList<>();  // new list!
        b.add(4);
        b.add(5);
        l.add(b);
        System.out.println(l+" | ");
    }
}

Background: When you create the list a, that is an object. You then add elements to that list. Then you add list a to l. l now has a reference to a, it does not make a copy of a. Then you clear a, which means l now has a reference to the empty list a. Then you add new items to a, and add a to l again. That means that l now has two references to the same list a. Hence when you print l, it prints a twice.

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

2 Comments

Could you not reuse a? Not the ArrayList a initially references, but after storing a reference to that ArrayList in l, can you not assign a new ArrayList to a?
@user4581301 Yes, good point, that would work too. I made a new variable in the hope to make it clearer for a beginner.
2

As @Robert has explained, a in the original code is a single reference added several times to the list l. However, it is possible to simply create a copy of a list when adding it to the outer list using its constructor:

List<List<Integer>> l = new ArrayList<>();
List<Integer> a = new ArrayList<>();
a.add(1);
a.add(2);
l.add(new ArrayList<>(a)); // copy added to l

a.clear();
a.add(4);
a.add(5);
l.add(new ArrayList<>(a)); // another copy added to l
System.out.println(l+" | ");

Another approach would be to avoid creating a temporary list a at all and populate list l using Arrays.asList which creates a fixed-size list:

List<List<Integer>> l = new ArrayList<>(); // modifiable

l.add(Arrays.asList(1, 2));
l.add(Arrays.asList(4, 5));

System.out.println(l+" | ");

Comments

-1

you are cleaning the list before printing, you can type the below code:

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        List<List<Integer>> l = new ArrayList<List<Integer>>();
        List<Integer> y = new ArrayList<Integer>();
        y.add(1);
        y.add(2);
        l.add(y);
        List<Integer> x = new ArrayList<>();  
        x.add(4);
        x.add(5);
        l.add(x);
        System.out.println(l+" | ");
    }
}

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.