tags:

views:

177

answers:

4

I need to create a copy of an already existing tree , created using DefaultMutableTreeNode.[Edit] So, I have tried to assign the existing root node, to another DefaultMutableTreeNode.Ex:

DefaultMutableTreeNode ABC = new DefaultMutableTreeNode(null);
DefaultMutableTreeNode ABCcopy = new DefaultMutableTreeNode(null);
ABCcopy=ABC;

But this didnt give me much results. Please advice.

+1  A: 

the easiest way to (deep) copy/clone an object in java is by serializing/deserializing it.

nkr1pt
@nkr1pt : could you post some code, please?
fixxxer
A: 

How about

Tree newTree = existingTree.clone() ?
NA
You need to cast: clone() returns an Object. Also, the OP should know that all objects inside his/her tree should implement the Cloneable interface.
Bart Kiers
seriously, save yourselves the trouble from implementing the Clonebale interface in the object graph of the objects you want to clone. Use the built-in serialize mechanism to create deep clones.
nkr1pt
@nkr1pt : could you post some code, please?
fixxxer
A: 

Here is an example:

FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
   fos = new FileOutputStream("somefilename");
   out = new ObjectOutputStream(fos);
   out.writeObject(ABC);
   out.close();
} catch(IOException ex) {
  ex.printStackTrace();
}

FileInputStream fis = null;
ObjectInputStream in = null;
try {
   fis = new FileInputStream("somefilename");
   in = new ObjectInputStream(fis);
   ABCCopy = (DefaultMutableTreeNode)in.readObject();
   in.close();
} catch(IOException ex) {
   ex.printStackTrace();
} catch(ClassNotFoundException ex) {
   ex.printStackTrace();
}
nkr1pt
Lets suppose that a tree exists with the root node as ABC. How do we proceed with the above code, then? –
fixxxer
Exactly as in the example. Read it carefully, the out.writeObject(ABC) serializes the existing tree to the filesystem and ABCCopy = (DefaultMutableTreeNode)in.readObject() deserializes the tree into the ABCCopy variable. At that point you should have two identical trees.
nkr1pt
What will be the contents of "somefilename"? Would any file format do?
fixxxer
yes never mind the content of "somefilename", it is not human-readable, I would just call it "temp".
nkr1pt
also, bear in mind that the ObjectOutputStream accepts a ByteArrayOutputStream instead of a FileOutputStream. This eliminates the need for disk IO and thus for a filename to write to.The same applies to the InputStreams
nkr1pt
thanks! but i think I found a easier solution.
fixxxer
A: 

I had written a function to populate a tree beginning with ABC. After which, I gave :

ABCcopy=ABC

And it has worked for me!

Thanks, everyone who contributed here, especially nkr1pt.

fixxxer
please note that now you have 2 pointers to 1 object, this means that you don't have a copy of the tree but simply have 2 different variables pointing to one and the same tree.
nkr1pt
but,by creating two objects, I will have two sets of values, which are independent of each other, right? That does translate to, having a copy of the tree,doesn't it?
fixxxer