ArrayList的深拷贝与浅拷贝

时间:2021-08-04 19:54:09

浅拷贝:被复制对象的任何变量都含有和原来的对象相同的值,而任何的对其他对象的引用仍然指向原来的对象。对拷贝后的引用的修改,还能影响原来的对象。 

深拷贝:把要复制的对象所引用的对象都复制了一遍,对现在对象的修改不会影响原有的对象。


介绍四种浅拷贝与一种深拷贝。

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

/**
*4种浅层拷贝
*1种深层拷贝
*/
public class Test1 {

/**
* 浅层copy1
*/
public void copy1(ArrayList<Person> oldList){
ArrayList<Person> newList = new ArrayList<Person>();
newList=(ArrayList<Person>) oldList.clone();
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
oldList.remove(0);
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
oldList.get(0).name="WYM";
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
}
/**
* 浅层copy2
*/
public void copy2(ArrayList<Person> oldList){
ArrayList<Person> newList = new ArrayList<Person>();
for(Person o :oldList){
newList.add(o);
}
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
oldList.remove(0);
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
oldList.get(0).name="WYM";
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
}
/**
* 浅层copy方法3
*/
public void copy3(ArrayList<Person> oldList){
ArrayList<Person> newList = new ArrayList<Person>();
newList.addAll(oldList);
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
oldList.remove(0);
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
oldList.get(0).name="WYM";
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
}
/**
* 浅层copy4
*/
public void copy4(ArrayList<Person> oldList){
ArrayList<Person> newList = new ArrayList<Person>(Arrays.asList(new Person[oldList.size()]));
Collections.copy(newList, oldList);
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
oldList.remove(0);
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
oldList.get(0).name="WYM";
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
}
/*
* 深层拷贝
*/
public void deepCopy(ArrayList<Person> oldList)throws Exception{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(baos);
oos.writeObject(oldList);
ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bais);
ArrayList<Person> newList=(ArrayList<Person>)ois.readObject();
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
oldList.remove(0);
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);
oldList.get(0).name="WYM";
System.out.println("oldList:"+oldList);
System.out.println("newList:"+newList);

}
public static void main(String[] args)throws Exception{
Test1 test = new Test1();
ArrayList<Person> list = new ArrayList<Person>();
list.add(new Person("A",1));
list.add(new Person("B",2));
list.add(new Person("C",3));
//test.copy1(list);
//test.copy2(list);
//test.copy3(list);
//test.copy4(list);
test.deepCopy(list);
}
}
class Person implements Serializable{
String name;
int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "姓名:"+this.name+"-年龄:"+this.age;
}
}