List<List<Integer>> list = new LinkedList<List<Integer>>();
List<Integer> tmp = new LinkedList<Integer>();
tmp.add(2);
list.add(tmp);
tmp.add(3);
list.add(tmp);
The result of list is [[2,3],[2,3]];
I just confused about that why it is not [[2],[2,3]]
. And when I use list.add(new LinkedList<Integer>(tmp))
it will work. I print tmp, it is still [2], [2,3]
, it is not changed. Why that happen?
Thank you in advance.