Nirvana
Nirvana

Reputation: 31

How make a List of ArrayList in Java?

I'm trying to make a list of lists, but I noticed that my list ends up empty at the end of the loop.

ArrayList<ArrayList<Integer>> list_of_lists = new ArrayList<ArrayList<Integer>>();
    List<Integer> list = new ArrayList<Integer>();          
    for(int x = 0; x < 5; x++) {
        for(int y = 0; y < 2; y++) {                
            list.add(x);
            list.add(y);                
            list_of_lists.add((ArrayList<Integer>) list);
            System.out.println(list_of_lists);
            list.clear();
        }
    }
    System.out.println(list_of_lists);

But, in console the result is:

[[0, 0]]
[[0, 1], [0, 1]]
[[1, 0], [1, 0], [1, 0]]
[[1, 1], [1, 1], [1, 1], [1, 1]]
[[2, 0], [2, 0], [2, 0], [2, 0], [2, 0]]
[[2, 1], [2, 1], [2, 1], [2, 1], [2, 1], [2, 1]]
[[3, 0], [3, 0], [3, 0], [3, 0], [3, 0], [3, 0], [3, 0]]
[[3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1]]
[[4, 0], [4, 0], [4, 0], [4, 0], [4, 0], [4, 0], [4, 0], [4, 0], [4, 0]]
[[4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1]]
[[], [], [], [], [], [], [], [], [], []]

Why, the final is a empy list insteed of :

[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1], [4, 0], [4, 1]]

It's with each other instead of two separate lists. My code was linking one list to another

Upvotes: 1

Views: 44

Answers (2)

Sheetal gupta
Sheetal gupta

Reputation: 311

The reason for being the last list empty is that Java stores everything in the form of references i.e in the form of memory address and manipulates them in the same way.

Hence ,the ArrayList object list is also a reference. So

1) Everytime the inner loop is run a reference to list object is added to the ArrayList list_of_lists.

2) Hence each time the list is manipulated the list_of_lists stores the value of list as many times as the inner loop is run i.e. the list is added to it.

Hence Each time System.out.println(list_of_lists) is called

1) it prints list same no of times it is added to list_of_lists.

2) the list is cleared with list.clear() after that which makes all the references to list empty.

You can also check this code to get list_of_lists at each iteration to get a clear picture of what is happening.

ArrayList<ArrayList<Integer>> list_of_lists = new ArrayList<ArrayList<Integer>>();
List<Integer> list = new ArrayList<Integer>();          
for(int x = 0; x < 5; x++) {
    for(int y = 0; y < 2; y++) {                
        list.add(x);
        list.add(y);                
        list_of_lists.add((ArrayList<Integer>) list);
        System.out.println(list_of_lists);
        list.clear();
    }
    System.out.println(list_of_lists); //composes of empty lists 
                                        //the same of times list is added to it
}
System.out.println(list_of_lists);

**Solution:*

To create a List of Lists you have to allocate memory each time new list is added to list_of_lists:

  ArrayList<ArrayList<Integer>> list_of_lists = new 
    ArrayList<ArrayList<Integer>>();
        List<Integer> list;        
        for(int x = 0; x < 5; x++) {
            for(int y = 0; y < 2; y++) {  
                list = new ArrayList<>();              
                list.add(x);
                list.add(y);                
                list_of_lists.add((ArrayList<Integer>) list);
                System.out.println(list_of_lists);
                // list.clear();
            }
         }
          System.out.println(list_of_lists);
        }

Upvotes: 1

Wojciech Wirzbicki
Wojciech Wirzbicki

Reputation: 4352

It's because all your sublists points to the same instance. You should create new instance for each pair of numbers instead of reusing list. Check the code below

    ArrayList<ArrayList<Integer>> list_of_lists = new ArrayList<ArrayList<Integer>>();
    List<Integer> list;
    for(int x = 0; x < 5; x++) {
        for(int y = 0; y < 2; y++) {
            list = new ArrayList<Integer>();
            list.add(x);
            list.add(y);
            list_of_lists.add((ArrayList<Integer>) list);
            System.out.println(list_of_lists);
            //list.clear();
        }
    }
    System.out.println(list_of_lists);

The output will be

[[0, 0]]
[[0, 0], [0, 1]]
[[0, 0], [0, 1], [1, 0]]
[[0, 0], [0, 1], [1, 0], [1, 1]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1], [4, 0]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1], [4, 0], [4, 1]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1], [4, 0], [4, 1]]

Upvotes: 1

Related Questions