java List深拷貝、淺拷貝
package test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import test.ListTest.Pro;
public class CpoyListTest implements Serializable{
/**
* 本次測試的list中儲存的值為String、Integer型別
* @author admin
*
*/
static class StringInteger{
public static void main(String[] args) {
StringInteger stringInteger = new StringInteger();
stringInteger.DeepCopy();
stringInteger.ShallowCopy();
}
/**
* 深拷貝
*/
public void DeepCopy() {
System.out.println("---------------------------DeepCopy------Begin");
//建立資料來源:
List<Object> srcList = new ArrayList<Object>();
srcList.add(1);
srcList.add(2);
//第一種:
List<Object> destList1= new ArrayList<Object>(srcList);
//第二種:
List<Object> destList2= Arrays.asList(new Object[srcList .size()]);
Collections.copy(destList2, srcList);
//第三種:
List<Object> destList3= new ArrayList<Object>();
destList3.addAll(srcList);
//第四種:
List<Object> destList4= new ArrayList<Object>();
for (int i = 0; i < srcList.size(); i++) {
destList4.add(srcList.get(i));
}
//測試:
System.out.println("源list"+srcList);//輸出[1, 2]
System.out.println("1copyList:"+destList1);//輸出[1, 2]
System.out.println("2copyList:"+destList2);//輸出[1, 2]
System.out.println("3copyList:"+destList3);//輸出[1, 2]
System.out.println("4copyList:"+destList4);//輸出[1, 2]
System.out.println("---刪除源資料第一個元素");
srcList.remove(0);
System.out.println("---刪除源資料第一個元素之後");
System.out.println("源list"+srcList);//輸出[2]
System.out.println("1copyList:"+destList1);//輸出[1, 2]
System.out.println("2copyList:"+destList2);//輸出[1, 2]
System.out.println("3copyList:"+destList3);//輸出[1, 2]
System.out.println("4copyList:"+destList4);//輸出[1, 2]
}
/**
* 淺拷貝
*/
public void ShallowCopy() {
System.out.println("---------------------------ShallowCopy------Begin");
//建立資料來源:
List<Object> srcList = new ArrayList<Object>();
srcList.add(1);
srcList.add(2);
List<Object> destList5= new ArrayList<Object>();
destList5 = srcList;
System.out.println("源list"+srcList);//輸出[1, 2]
System.out.println("5copyList:"+destList5);//輸出[1, 2]
System.out.println("---刪除源資料第一個元素");
srcList.remove(0);
System.out.println("---刪除源資料第一個元素之後");
System.out.println("源list"+srcList);//輸出[2]
System.out.println("5copyList:"+destList5);//輸出[2]
}
}
static class CustomObject{
public static void main(String[] args) {
try {
CustomObject customObject = new CustomObject();
customObject.DeepCopy();
customObject.ShallowCopy();
} catch (Exception e) {
e.printStackTrace();
}
}
//序列化實現
@SuppressWarnings("unchecked")
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);
List<T> dest = (List<T>) in.readObject();
return dest;
}
/**
* 深拷貝
* @throws IOException
* @throws ClassNotFoundException
*/
public void DeepCopy() throws ClassNotFoundException, IOException {
System.out.println("---------------------------DeepCopy------Begin");
//建立源資料
List<Pro> srcList = new ArrayList<Pro>();
Pro pro = new CpoyListTest().new Pro();
pro.setAge(1);
srcList.add(pro);
//序列化方式
List<Pro> destList1 = deepCopy(srcList);
//普通迴圈建立物件方式
List<Pro> destList2= new ArrayList<Pro>();//深拷貝
for (int i = 0; i < srcList.size(); i++) {
Pro pro2 = new CpoyListTest().new Pro();
pro2.setAge(srcList.get(i).getAge());
destList2.add(pro2);
}
System.out.println("源listAge:"+srcList.get(0).getAge());//輸出1
System.out.println("1copyListAge:"+destList1.get(0).getAge());//輸出1
System.out.println("2copyListAge:"+destList2.get(0).getAge());//輸出1
System.out.println("---修改物件屬性");
srcList.get(0).setAge(2);
System.out.println("---修改物件屬性之後");
System.out.println("源listAge:"+srcList.get(0).getAge());//輸出2
System.out.println("1copyListAge:"+destList1.get(0).getAge());//輸出1
System.out.println("2copyListAge:"+destList2.get(0).getAge());//輸出1
}
/**
* 淺拷貝
*/
public void ShallowCopy() {
System.out.println("---------------------------ShallowCopy------Begin");
//建立源資料
List<Pro> srcList = new ArrayList<Pro>();
Pro pro = new CpoyListTest().new Pro();
pro.setAge(1);
srcList.add(pro);
//第一種:
List<Pro> destList1= new ArrayList<Pro>(srcList);
//第二種:
List<Pro> destList2= Arrays.asList(new Pro[srcList .size()]);
Collections.copy(destList2, srcList);
//第三種:
List<Pro> destList3= new ArrayList<Pro>();//淺拷貝
destList3.addAll(srcList);
//第四種:
List<Pro> destList4= new ArrayList<Pro>();//淺拷貝
for (int i = 0; i < srcList.size(); i++) {
destList4.add(srcList.get(i));
}
//第五種:
List<Pro> destList5= new ArrayList<Pro>();
destList5 = srcList;
System.out.println("源listAge:"+srcList.get(0).getAge());//輸出1
System.out.println("1copyListAge:"+destList1.get(0).getAge());//輸出1
System.out.println("2copyListAge:"+destList2.get(0).getAge());//輸出1
System.out.println("3copyListAge:"+destList3.get(0).getAge());//輸出1
System.out.println("4copyListAge:"+destList4.get(0).getAge());//輸出1
System.out.println("5copyListAge:"+destList5.get(0).getAge());//輸出1
System.out.println("---修改物件屬性");
srcList.get(0).setAge(2);
System.out.println("---修改物件屬性之後");
System.out.println("源listAge:"+srcList.get(0).getAge());//輸出1
System.out.println("1copyListAge:"+destList1.get(0).getAge());//輸出1
System.out.println("2copyListAge:"+destList2.get(0).getAge());//輸出1
System.out.println("3copyListAge:"+destList3.get(0).getAge());//輸出1
System.out.println("4copyListAge:"+destList4.get(0).getAge());//輸出1
System.out.println("5copyListAge:"+destList5.get(0).getAge());//輸出1
}
}
class Pro implements Serializable{
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
}
此連結講的非常清楚(包括評論上的資訊):http://will-turner.iteye.com/blog/1478194