在保持原有的數(shù)據(jù)不變的情況下盐杂,修改復(fù)制的list列表,而不影響原list瓷马。
public static <E> List<E> deepCopy(List<E> src) {
try {
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<E> dest = (List<E>) in.readObject();
return dest;
} catch (Exception e) {
e.printStackTrace();
return new ArrayList<E>();
}
}
List<E> 需要 E 對象實現(xiàn)接口 Serializable难捌,否則會報錯膝宁。