I have been trying out nested arrayList methods. I have a problem.
public static void main(String[] args) {
List<List<Integer>> list = new ArrayList<>();
List<Integer> subList = new ArrayList<Integer>(){{
add(1);
add(2);
}};
list.add(subList);
subList.clear();
subList.add(3);
subList.add(4);
list.add(subList);
System.out.println(list);
}
This does not give expected output. The result is
[[3, 4], [3, 4]]
and not [[1, 2], [3, 4]]
What is wrong with my Code.
EDIT: I have a few more issues on the code. Should I create a new quetsion or add it here.
list = [ sublist, sublist ]Note that you have only one sublist with two references to it.