I have two lists, ListA and ListB and I want to copy ListB to ListA.
What is the difference between the following, in terms of complexity and result:
ListA = ListB;ListA = new ArrayList(ListB);
I have two lists, ListA and ListB and I want to copy ListB to ListA.
What is the difference between the following, in terms of complexity and result:
ListA = ListB;ListA = new ArrayList(ListB);With listA = listB, there is only one list. So if you call listA.add(something);, it will add to listB too.
With listA = new ArrayList(listB); there are two lists (which happen to contain the same objects). So if you call listA.add(something);, it will NOT add anything to listB.
In terms of complexity, the first one is a simple assignement (i.e. runs in constant time), whereas the second runs in O(n), n being the size of listB.