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