Java的拷貝可以分為淺拷貝和深拷貝地回。
淺拷貝
原變量和拷貝變量引用同一個對象越锈,改變一個變量鎖引用的對象將會對另一個變量產(chǎn)生影響方灾。
Person original = new Person("johb",20);
Person copy = origin;
copy.setAge(21);
result: original only changed!!!
深拷貝(也叫做克隆)
如果想創(chuàng)建一個對象的copy议忽,它的初始狀態(tài)和original一樣溉跃,但是以后可以改變各自的狀態(tài)村刨,那就需要使用clone方法
clone是Object的protected方法,Object中的clone方法喊积,由于object類對具體的類對象一無所知烹困,所以只能講各個域進行對應(yīng)的拷貝。如果對象中的數(shù)據(jù)域都是數(shù)值或者基本數(shù)據(jù)類型乾吻,這樣的拷貝沒有問題髓梅,如果對象中包含了子對象拟蜻,那么拷貝的結(jié)果會使得兩個域引用同一個子對象。默認(rèn)的克隆操作是淺拷貝枯饿,沒有克隆包含在對象中的內(nèi)部對象酝锅。
而且,由于clone方法是保護方法奢方,所以用戶不能直接調(diào)用它搔扁,需要在類中重新定義clone方法。
public class Person implements Cloneable{
public Employee clone() throws CloneNotSupportedException {
return (Employee )super.clone();
}
}
上述clone方法沒有在Object.clone()
提供的淺拷貝的基礎(chǔ)上新增任何東西蟋字,而只是將這個方法聲明為public稿蹲。為了實現(xiàn)深拷貝,必須克隆所有可變的實例域
public class Person implements Cloneable{
public Employee clone() throws CloneNotSupportedException {
Employee cloned = (Employee )super.clone();
cloned.hireDay = (Data)hireDay.clone;
return cloned;
}
}
當(dāng)子對象可變鹊奖,必須重新定義克隆方法苛聘,以便實現(xiàn)克隆子對象的深拷貝
數(shù)組鏈表的拷貝
數(shù)組鏈表也是一種對象,若元素不是基本數(shù)據(jù)類型時忠聚,相當(dāng)于內(nèi)部對象设哗,因此以下幾種方式都是淺拷貝。原始鏈表和拷貝鏈表共享元素两蟀。
幾種淺拷貝
遍歷循環(huán)復(fù)制
List<Person> srcList = new ArrayList<>();
srcList.add(new Person("jack", 24));
srcList.add(new Person("rose", 22));
List<Person> destList = new ArrayList<>(srcList.size());
for (Person p : srcList) {
destList.add(p);
}
使用List實現(xiàn)類的構(gòu)造方法
List<Person> srcList = new ArrayList<>();
srcList.add(new Person("jack", 24));
srcList.add(new Person("rose", 22));
List<Person> destList1 = new ArrayList<Person>(srcList);
list.addAll方法
List<Person> srcList = new ArrayList<>();
srcList.add(new Person("jack", 24));
srcList.add(new Person("rose", 22));
List<Person> destList2 = new ArrayList<Person>();
destList2.addAll(srcList);
System.arraycopy方法
List<Person> srcList = new ArrayList<>();
srcList.add(new Person("jack", 24));
srcList.add(new Person("rose", 22));
List<Person> destList = new ArrayList<>(srcList.size());
Person[] srcPersons = srcList.toArray(new Person[0]);
Person[] destPersons = new Person[srcPersons.length];
System.arraycopy(srcPersons, 0, destPersons, 0, srcPersons.length);
其實跟進去java代碼网梢,發(fā)現(xiàn)上述三種本質(zhì)都是調(diào)用了System.arraycopy
方法
上述拷貝方法,改變了A的元素的屬性赂毯,會改變B战虏。
srcList.get(0).setName("change!!");
不過,直接對A進行新增元素等操作欢瞪,并不會影響B(tài)活烙。
srcList.add(new Person("gga",23));
數(shù)組鏈表的深拷貝
序列化
public static <T> List<T> deepCopy(List<T> 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);
@SuppressWarnings("unchecked")
List<T> dest = (List<T>) in.readObject();
return dest;
}
List<Person> destList=deepCopy(srcList); //調(diào)用該方法
clone方法
public class A implements Cloneable {
public String name[];
public A(){
name=new String[2];
}
public Object clone() {
A o = null;
try {
o = (A) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
for(int i=0;i<n;i+=){
copy.add((A)src.get(i).clone());
}
參考
java List復(fù)制:淺拷貝與深拷貝
java核心技術(shù)卷1 P76 P213