If I create a list and add ten elements then copy the list object to another and call list.clear on the second list, both of the lists are cleared.
Random random = new Random();
List<Double> list1 = new ArrayList<Double>();
List<Double> list2 = new ArrayList<Double>();
for (int i = 0; i < 10; i++) {
list1.add(random.nextDouble());
}
list2 = list1; //Both lists have ten elements
list1.clear(); //Both lists are now empty
Is this normal? Am I missing something?
If I iterate through the first list and add the elements one by one this does not occur, only with the list2 = list1
line.