I created a list of contacts Inserted into database Create a new list and passed the previous list as a constructor parameter. Now when i make any changes in my new list,it gets reflected in my previous list as well.Why is it so.
I thought when you declare a new list with 'New' key word it got its own Heap area.and whatever changes happens,happens only in its own heap.and changes are local for it.
Code :
list<Contact> cts = new list<contact>();
for(integer x =0;x<5;x++){
cts.add(new contact(lastname = 'cttest_'+string.valueof(x)));
}
insert cts;
map<contact,Integer> contactmap = new map<contact,integer>();
for (Integer x = 0; x<5;x++){
contactmap.put(cts[x],x);
}
system.assertequals(contactmap.size(),5);
//system.debug('Limits:' + limits.getheapsize());
List<contact> samecontacts = new list<contact>(cts);
//system.debug('Limits:' + limits.getheapsize());
system.debug(samecontacts);
for(Integer x = 0; x<5;x++){
samecontacts[x].assistantname = 'person'+string.valueof(x);
system.assertequals(cts[x].assistantname,samecontacts[x].assistantname);
}