在实际的编程过程中,我们常常会发现这样一个问题:
两个对象中的内容是一模一样的,但是修改其中一个对象时,另外一个对象中的值也会相应的改变,这是因为在java的对象Clone方法中,赋值的是对象的引用地址,而不是对象中的具体的数据值,要的到一模一样的数据且相互之间不产生影响,这就涉及到对象的深层拷贝,
对象的深层拷贝有以下两种方法可以实现:
1、对Clone的对象实现Cloneable的接口,同时重写Clone方法
package DeepCopy;
public class Person implements Cloneable{
private String name;
private Double salary;
private int workAge;
@Override
protected Object clone() throws CloneNotSupportedException {
Person person=new Person();
person.name=name;
person.salary=salary;
person.workAge=workAge;
return person;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public int getWorkAge() {
return workAge;
}
public void setWorkAge(int workAge) {
this.workAge = workAge;
}
}
调用方式如下:
package DeepCopy;
import java.util.ArrayList;
import java.util.List;
public class DeepTestDemo {
public static List<Person> CloneMethod(List<Person> sources)
throws CloneNotSupportedException {
ArrayList<Person> targetpersonList = new ArrayList<Person>();
for (Person Per : sources) {
targetpersonList.add((Person) Per.clone());
}
return targetpersonList;
}
public static void main(String[] args) throws CloneNotSupportedException {
ArrayList<Person> sourcepersonList = new ArrayList<Person>();
for (int i = 0; i <= 10; i++) {
Person per = new Person();
per.setName("Person#" + i);
per.setSalary(Double.valueOf(i));
per.setWorkAge(i + 10);
sourcepersonList.add(per);
}
System.out.println("原始数据List存储地址:" + sourcepersonList.toString());
System.out.println("Clone之后新数据List的存储地址:"
+ CloneMethod(sourcepersonList).toString());
}
}
测试结果如下:
原始数据List存储地址:[DeepCopy.Person@1fb8ee3, DeepCopy.Person@61de33, DeepCopy.Person@14318bb, DeepCopy.Person@ca0b6, DeepCopy.Person@10b30a7, DeepCopy.Person@1a758cb, DeepCopy.Person@1b67f74, DeepCopy.Person@69b332, DeepCopy.Person@173a10f, DeepCopy.Person@530daa, DeepCopy.Person@a62fc3]
Clone之后新数据List的存储地址:[DeepCopy.Person@89ae9e, DeepCopy.Person@1270b73, DeepCopy.Person@60aeb0, DeepCopy.Person@16caf43, DeepCopy.Person@66848c, DeepCopy.Person@8813f2, DeepCopy.Person@1d58aae, DeepCopy.Person@83cc67, DeepCopy.Person@e09713, DeepCopy.Person@de6f34, DeepCopy.Person@156ee8e]
2、对Clone的对象实现Serializable的接口
package DeepCopy;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
使用方法
package DeepCopy;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class DeepTestDemo2 {
public static List deepCopy(List src) throws IOException,
ClassNotFoundException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(
byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
List dest = (List) in.readObject();
return dest;
}
public static void main(String[] args) throws IOException,
ClassNotFoundException {
ArrayList<Student> sourcestuList = new ArrayList<Student>();
for (int i = 0; i <= 10; i++) {
Student stu = new Student();
stu.setName("stu#" + i);
stu.setAge(i);
sourcestuList.add(stu);
}
System.out.println("原始数据List存储地址:" + sourcestuList.toString());
System.out.println("DeepCopy之后新数据List的存储地址:"
+ deepCopy(sourcestuList).toString());
}
}
测试结果
原始数据List存储地址:[DeepCopy.Student@1fb8ee3, DeepCopy.Student@61de33, DeepCopy.Student@14318bb, DeepCopy.Student@ca0b6, DeepCopy.Student@10b30a7, DeepCopy.Student@1a758cb, DeepCopy.Student@1b67f74, DeepCopy.Student@69b332, DeepCopy.Student@173a10f, DeepCopy.Student@530daa, DeepCopy.Student@a62fc3]
DeepCopy之后新数据List的存储地址:[DeepCopy.Student@32c41a, DeepCopy.Student@e89b94, DeepCopy.Student@13e205f, DeepCopy.Student@1bf73fa, DeepCopy.Student@5740bb, DeepCopy.Student@5ac072, DeepCopy.Student@109a4c, DeepCopy.Student@201f9, DeepCopy.Student@1cf8583, DeepCopy.Student@14693c7, DeepCopy.Student@901887]