0

This may be a too-specific question, but I'm hoping there's a more general solution to my problem.


I have a class. In this class is a tree-type structure with many parent/child nodes. Also in this class is an Array filled with references to each node in this tree-type structure.


The tree's purpose is for each node to know where to draw itself on screen (every node has relative positional information based on its parent's location).

The Array's purpose is the draw order, I simply draw whatever node is referenced at Array[0] first and so on. (So, the nodes aren't being drawn in the order they appear in the tree necessarily).


My problem is this. I want to clone this overall class that contains these two objects (tree with nodes and an Array that references said nodes). This seems simple.

I create a deep copy of the tree structure and the nodes it contains. Cool.

However, I don't know how to repopulate a new Array with references to these new nodes in this new tree. It seems like it would be simple but I can't figure out how to do it.

I tried to be specific enough to give enough information without being too confusing, hope you understand.

Thanks.

yesbutmaybeno
  • 1,078
  • 13
  • 31
  • sry i didn't get what your problem is. – PKlumpp May 15 '14 at 13:00
  • If you implement Clone you will be calling `object.Clone()` that will return a new instance; if your object has a list look at this post http://stackoverflow.com/questions/715650/how-to-clone-arraylist-and-also-clone-its-contents – Kenneth Clark May 15 '14 at 13:11

1 Answers1

0

If you're able to change the node data structure, you could add a field for the node's array index. That way, once you've rebuilt your tree you can just walk through it and use the index field to repopulate the array. Not super elegant, but it gets the job done...

Or, to avoid adding a field to your node class, I suppose you could use a temporary hashtable that maps nodes to array indices. Walk through your source array to populate the hashtable, then once you've cloned the tree, walk through the tree, looking up the new nodes in the hashtable (which will work fine assuming you've implemented equals and hashCode properly) and populating the array from those.

adv12
  • 8,443
  • 2
  • 24
  • 48