浅拷贝是平常用的最多的,比如
源list
List srcList = new ArraryList();
目标list
List list = new ArraryList();
list.addAll(srcList);
Collections.copy(list,srcList);
浅拷贝拷贝的是list的地址,所以改变list的时候srcList也会改变
这时就需要深拷贝
下面是通用深拷贝方法,网上其他方式的深拷贝都没啥用
static List deepCopy(List srcList){
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream objectOut = new ObjectOutputStream(byteOut);
objectOut.writeObject(src);
ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOut.toByteArray);
ObjectInputStream objectInput = new ObjectInputStream(byteInput);
return (List)objectInput.readObject();
}