The operation clonein ArrayListreturns a shallow copy of the object and is not suitable for your purposes. Manual Workaround:
- Create a list of target arrays of the same size as the source list
- Iterate the source list and create a clone of each of its items in the goal list
, , , clone, , , clone . , . , Java , . Java: / SO . , :
() , :
public MyGraph deepCopy() {
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(256);
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
oos.close();
final ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream(baos.toByteArray()));
final MyGraph clone = (QuicksortTest) ois.readObject();
return clone;
} catch (final Exception e) {
throw new RuntimeException("Cloning failed");
}
}
, Java / , . .
, Dozer . Orika , :
public MyGraph deepCopy() {
final DozerBeanMapper mapper = new DozerBeanMapper();
final QuicksortTest clone = mapper.map(this, MyGraph.class);
return clone;
}
, , , .
deepCopy . , , , /.