Eigenständige Kopie eines Objektes mitsammt seiner SubObjekte erstellen

GoldKaetzchen

Grünschnabel
Hallo,
ich habe folgendes Problem. Ich muss ein Objekt verändern und schauen wie es nach dieser Veränderung aussieht aber danach wieder den Ur-Zustand herstellen und das komplett! Ich habe in dem Objekt noch viele Unterobjekte, die wiederum Objekte besitzen diese Unterobjekte geben sich untereinander wieder die Objekte rund (das muss ich mir anschauen) aber nachher muss ich halt wieder alles zurücknehmen können. Ich dachte mir ich mache eine komplette Kopie von dem Ding und lasse das unter einer anderen Referenz laufen und dann überschreibe ich diese Dummy-Referenz wieder mit einer Kopie des unveränderten Originals.
Nur habe ich keine Ahnung wie ich alle Objekte so schnell Kopiert bekomme und nicht nur die Referenzen (mit clone() ) hat da jemand eine Idee

:confused:
 
Hallo zerix,

zerix hat gesagt.:
Mit clone() kopiert man soweit ich weiß das objekt

leider stimmt das nur oberflächlich, ich muss auch alle subObjekte kopieren und da macht clone() alleine nicht mit, man muss also irgendwie noch clone() überschreiben mit einer deep copy Funktion aber ich hab keine Ahnung wie.
In der API zu clone() (von Object) steht jedenfalls folgendes:
Java API 1.42 hat gesagt.:
By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

hat jemand da vielleicht schon mal was gemacht und weiss was die damit meinen?

:)
 
Hallo,

fuer eine Tiefekopie benutze ich immer gerne folgenden Code:

Code:
  public static Object tiefekopie(final Object source) throws IOException,ClassNotFoundException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(
        byteArrayOutputStream);
    objectOutputStream.writeObject(source);
    objectOutputStream.close();
    byte[] buffer = byteArrayOutputStream.toByteArray();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
        buffer);
    ObjectInputStream objectInputStream = new ObjectInputStream(
        byteArrayInputStream);
    return (Object) objectInputStream.readObject();
  }

Gruß,
Matthias

Falls eine Instanzvariable mal nicht mit gekopiert werden soll einfach transient deklarieren.
 

Neue Beiträge

Zurück