7

friends,

i am facing a problem i have phoneContacts list with name and phone numbers in it. i want to copy it in two different static lists so that i can use it to other activities. i am using following code but it displays me last list references in both while retrieving data any one guide me how can i take separate copies of these two objects ?

MyContacts.attackContacts = new ArrayList(phoneContacts);
Collections.copy(MyContacts.attackContacts,phoneContacts);
MyContacts.attackContacts.get(0).setType("attack");

MyContacts.medicalContacts  = new ArrayList(phoneContacts);
Collections.copy(MyContacts.medicalContacts,phoneContacts);
MyContacts.medicalContacts.get(0).setType("medical");

System.out.println("attack" + MyContacts.attackContacts.get(0).getType() + " medical " + MyContacts.medicalContacts.get(0).getType());

// result "attack medical" "medical medical"
// it should show independent list results like "attack attack" "medical medical"

any help would be appreciated.

aioobe
  • 413,195
  • 112
  • 811
  • 826
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

3 Answers3

10

In this case you would need to make a deep copy of the list, i.e., a copy that doesn't copy references, but actually copies the object the references are pointing at.

Collections.copy "copies all of the elements from one list into another". As usual with Java however, the elements of a list are not objects but references.

You could solve this by implementing Cloneable (and using .clone()) or by creating a custom "copy constructor" which takes a to-be-copied object as argument, and creates a new object based on the data of that argument. No matter which option you choose, you'll have to iterate over the list and perform the copy on each object.

Here's an example that uses the copy-constructor approach:

MyContacts.medicalContacts = new ArrayList();
for (Contact c: MyContacts.attackContacts)
    medicalContacts.add(new Contact(c));    // add a copy of c.

Related question:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • thanks for reply any example or tutorial? i have no idea of it so using clone each list will point to another new reference and i will get result as i mentioned above? – UMAR-MOBITSOLUTIONS Jun 01 '11 at 08:43
  • *each list will point to another new reference and i will get result as i mentioned above?* Exactly, you got it. Added an example. – aioobe Jun 01 '11 at 08:44
  • i got right tutorial thanks for pointing me out http://www.java2s.com/Code/Java/Language-Basics/DeepCopyTest.htm – UMAR-MOBITSOLUTIONS Jun 01 '11 at 09:53
1

Coping the list, creates new list object that still refers to the same element objects. To make a deep copy, your element object must be clonable, have copy constructor, or some other way to duplicate it and you have to do the copy in a loop, one by one.

for (Elem x: list1) {  
  list2.add(copyOf(x))  
}
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53
0

Pass the object whcih you wants to copy and get the object which you wants ,

private Object copyObject(Object objSource) {
  try {
   ByteArrayOutputStream bos = new ByteArrayOutputStream();
   ObjectOutputStream oos = new ObjectOutputStream(bos);
   oos.writeObject(objSource);
   oos.flush();
   oos.close();
   bos.close();
   byte[] byteData = bos.toByteArray();
   ByteArrayInputStream bais = new ByteArrayInputStream(byteData);
   try {
    objDest = new ObjectInputStream(bais).readObject();
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  return objDest;

 }

Cast you objDest to Desiered object

A-Droid Tech
  • 2,301
  • 24
  • 33