I am passing in to a callback method (List a) and I want to know how to copy over the value.
List<addresses> mInstanceList;
public void setMyList(List<Address> addresses)
{
mInstanceList = addresses; // is this going to work?
}
I am passing in to a callback method (List a) and I want to know how to copy over the value.
List<addresses> mInstanceList;
public void setMyList(List<Address> addresses)
{
mInstanceList = addresses; // is this going to work?
}
Do you mean new copy? If so, you may do something like mInstanceList=new Arraylist(addresses);
There you are copying the reference to the list that addresses points to mInstanceList. If someone else changes the elements of that list, it will be reflected in mInstanceList. If you don't want that, you can create a new list (maybe an ArrayList) and copy the contents of the addresses list, in the same order.
mInstanceList = new ArrayList<Address>();
for (Address a: addresses) {
mInstanceList.add(a);
}
1. If you want to copy a list and assign it to a List Reference Variable having its very own copy, then use Copy Constructor
.
Eg:
ArrayList<Address> list1 = new ArrayList<Address>();
ArrayList<Address> list2 = new ArrayList<Address>(list2);
Change made into the ArrayList object referred by list1 will not reflected into ArrayList object referred by list2, as they both are pointing to 2 different ArrayList object on the heap.
2. If you just want to make to List Reference Variable pointing on the same object on the heap, then do this..
ArrayList<Address> list1 = new ArrayList<Address>();
ArrayList<Address> list2 = list1 ;
As now both the references list1 and list2 are pointing on the same ArrayList object on the heap, changes made by one will be reflected in another.