UMAR-MOBITSOLUTIONS
UMAR-MOBITSOLUTIONS

Reputation: 77984

How to create copy of same object with different reference?

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.

Upvotes: 7

Views: 7492

Answers (3)

A-Droid Tech
A-Droid Tech

Reputation: 2321

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

Upvotes: 0

aioobe
aioobe

Reputation: 420921

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:

Upvotes: 10

Op De Cirkel
Op De Cirkel

Reputation: 29463

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))  
}

Upvotes: 1

Related Questions