list 浅,深拷贝
参考: https://www.cnblogs.com/luxd/p/11933686.html
public void simpleTest1() {
List<TestA> list = new ArrayList<>();
list.add(new TestA("aa"));
TestA testA = new ArrayList<>(list).get(0);
testA.setName("bb");
System.out.println(list.get(0));
List<TestA> objects = new ArrayList<>();
objects.addAll(list);
objects.get(0).setName("cc");
System.out.println(list.get(0));
TestA[] testAS = list.toArray(new TestA[0]);
TestA[] testA1 = new TestA[list.size()];
System.arraycopy(testAS, 0, testA1, 0, list.size());
testA1[0].setName("dd");
System.out.println(list);
List<TestA> arrayList = new ArrayList<>(1);
arrayList.add(new TestA("aaaaaa"));
List<TestA> objectss = new ArrayList<>(1);
System.out.println(objectss.size());
Collections.addAll(objectss, new TestA[arrayList.size()]);
Collections.copy(objectss, arrayList);
objectss.get(0).setName("bbbbb");
System.out.println(objectss);
System.out.println(arrayList);
}
public static <T> List<T> deepTest1(List<T> src) {
try (
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOut);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
) {
objectOutputStream.writeObject(src);
@SuppressWarnings("unchecked")
List<T> dest = (List<T>) objectInputStream.readObject();
return dest;
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}