I want to exercise the "Clear" utility of java. However, when I am using clear even after adding the sublist value to the super list, the values are somehow overwritten to null. What is the reason for this behavior?
public static void main(String [] args)
{
List<Integer> abc= new LinkedList<>();
List<List<Integer>> abc2= new LinkedList<>();
int j=2;
while(j > 0) {
for (int i = 1; i < 4; i++) {
abc.add(i);
}
//abc.stream().forEach(e-> System.out.println(e));
abc2.add(abc);
abc.clear();
//abc= new LinkedList<>();
j--;
}
abc2.stream().forEach(e-> System.out.println("," + e));
}
}
Why I am getting below result:
,[],[]
instead of:
,[1, 2, 3] ,[1, 2, 3]