java中ArrayList深拷贝有关问题
基本思路就是将要深拷贝的对象实现Serializable接口。
浅拷贝:被复制对象的任何变量都含有和原来的对象相同的值,而任何的对其他对象的引用仍然指向原来的对象。对拷贝后的引用的修改,还能影响原来的对象。
深拷贝:把要复制的对象所引用的对象都复制了一遍,对现在对象的修改不会影响原有的对象。
-------------------------------------------------
//浅拷贝与深拷贝
//浅拷贝:被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。
//换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。
//深拷贝:被复制对象的所有变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。
//那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。
//换言之,深复制把要复制的对象所引用的对象都复制了一遍。
//1、直接赋值(字符串外都属于浅拷贝)
//2、使用构造函数(深拷贝)
//3、使用clone()方法(深拷贝)
import java.io.Serializable;public class Person implements Serializable{private static final long serialVersionUID = -7622835197591599128L;private int age;private String name;public Person(){};public Person(String name,int age){this.name=name;this.age=age;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String toString(){return this.name+"-->"+this.age;}}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 Test {public static <T> void printList(List<T> list){System.out.println("---begin---");for(T t : list){System.out.println(t);}System.out.println("---end---");}public static <T> void printArray(T[] array){System.out.println("---begin---");for(T t : array){System.out.println(t);}System.out.println("---end---");}//关键代码 执行序列化和反序列化 进行深度拷贝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;} //关键代码 执行序列化和反序列化 进行深度拷贝,写法不同而已,作用一样 //个人习惯 怎么喜欢怎么来!public 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 {List<Person> srcList=new ArrayList<Person>();Person p1=new Person("123",20);Person p2=new Person("ABC",21);Person p3=new Person("abc",22);srcList.add(p1);srcList.add(p2);srcList.add(p3);List<Person> destList=deepCopy(srcList);printList(destList);srcList.get(0).setAge(100);System.out.println(srcList.get(0) == destList.get(0));printList(destList);printList(srcList); //这种复制也是浅拷贝,不能达到要求//Collections.copy(List des,List src)//List<Person> destList=new ArrayList<Person>( Arrays.asList(new Person[srcList.size()]));////List<Person> destList=new ArrayList<Person>(srcList.size());////List iss = new ArrayList(Arrays.asList(new Object[srcList.size()]));//////System.out.println(destList.size());//Collections.copy(destList,srcList );////srcList.get(0).setAge(100);//System.out.println(srcList.get(0) == destList.get(0));//printList(destList);//printList(srcList);}}