0

I've been trying to do deep copy of a 2 dimensional array but never succeeded. Here is my code.

class node {
    public node head;
    public node left;
    public node right;
    public node up;
    public node down;
}

node[][] OriginalArrayOfNode = new node[100][200];

//filling original node
for (int n = 0; n < 200; n++) {
     for(int m = 0; m < 100; m++) {
        OriginalArrayOfNode[m][n].head = OriginalArrayOfNode[m][0];
        OriginalArrayOfNode[m][n].left = ...
        //etc
     }
}

node[][]CopyArrayOfNode = new node[100][200];
//The code to copy the original array to new array should be here.

My question is how can i make a deep copy of my the OriginalArrayOfNode to CopyArrayOfNode ? Thanks in advance.

EDIT :

Im trying to make a copy of circular doubly-linked list with 4 pointers for Knuth's Dancing Link Algorithm. It's pretty hard to trace where is the problem, but i assume if the original-array give "x" as a result from Knuth's DL Algorithm, then a correct deep copy of the original-array will also give "x" as a result, provided that there are no other variable change and no random modifier. However, ive tried clone() method, arrayutil.copy() method, and none of them give a "correct" deep copy based on my assumption above.

JrL
  • 221
  • 1
  • 7
  • 19
  • What part are you having trouble with? What have you tried? Where are you stuck? – user1118321 Aug 09 '12 at 03:42
  • I am having trouble with copying my 2dimensional array. I've tried clone() but it didnt work. – JrL Aug 09 '12 at 03:55
  • I understand you're having trouble copying the 2 dimensional array. What trouble are you having copying it? What did you try that isn't working? Where are you stuck with the copying? You need to be more specific or it's hard to help. – user1118321 Aug 09 '12 at 03:59
  • Have a look at this SO question: http://stackoverflow.com/questions/3947227/deep-copy-of-an-object-array – Sujay Aug 09 '12 at 04:03
  • Thanks for the reply :) I've edited my question. Hope that i made my question clear enough :) – JrL Aug 09 '12 at 04:21
  • 1
    Can you explain how you aren't getting a NullPointerException on the first line of the body of your doubly-nested loop? `OriginalArrayOfNode` is a 2-d array of nulls after it's declared, and you never once allocate any new objects in this example. – Judge Mental Aug 09 '12 at 06:18
  • Yeah :) I got a NullPointerException on that. I just re-type my code here to make it look simpler. Since my original code looks to complicated and long. Originally, I allocated new node object on that 2-d array. – JrL Aug 09 '12 at 10:36

2 Answers2

1

In my opinion, you're copying this in a very strange way; almost like you're trying to copy the wrong way around.

I'd do it more like this:

for (int m = 0; n < 200; m++) {
     for(int n = 1; n < 100; n++) {
        OriginalArrayOfNode[m][n].head = OriginalArrayOfNode[m][0].head;
        OriginalArrayOfNode[m][n].left = OriginalArrayOfNode[m][0].left;
        //etc
     }
}

Note: You should start n at 1, as you're copying from as you're copying from 0 to the others.

What I would suggest you do however, is add into your class node a clone() method. Clone would then provide an exact copy of the original class.

class node {
    public node head;
    public node left;
    public node right;
    public node up;
    public node down;

    public node clone() {
        final node clonedNode = new node();
        node.head = this.head;
        node.left = this.left;
        node.right = this.right;
        node.up = this.up;
        node.down = this.down;
    }
}


for (int n = 1; n < 200; n++) {
    OriginalArrayOfNode[n] = OriginalArrayOfNode[m].clone(); }

That's not the exact code at all, but you get what I'm meaning.

Finally, another thing to note is that if you're trying to deep copy in the way you're doing, you could easily just populate from index 1 - 200 using ArrayUtil.copy(...).

Hope all this helps.

Sujay
  • 6,753
  • 2
  • 30
  • 49
Michael
  • 1,014
  • 1
  • 8
  • 23
  • Thanks for the reply :) Let me try it your way and i'll reply again if it works :) – JrL Aug 09 '12 at 04:24
  • I cant seem to make it works. Anyway, I think ill just mark your answer as accepted since I give up trying to make a deep copy and just re-declaring & re-filling a new array. Thanks anyway :) – JrL Aug 09 '12 at 04:44
  • Thanks mate. Sounds like you might be overcomplicating things anyway :) Hope you sort it out. – Michael Aug 09 '12 at 04:44
1

I presume the OriginalArrayOfNode contains nodes that refer to other nodes in the same array? You're not going to be able to do a deep copy in that case, unless you beef up the node data structure to contain its own 2d index. For example, if OriginalArrayOfNode[0][0].right happens to refer to OriginalArrayOfNode[15][27], you won't be able to figure out that you need to do a deep copy of index [15][27] from the old array to the new before assigning the result to CopyArrayOfNode[0][0].right unless you search the old array exhaustively using object identity.

Even if you could stomach doing that brute force search for all nodes, or you could modify the node data structure to contain its own index, there will also likely be cycles formed by following these links around, greatly complicating any attempt to determine the correct order to copy things. If you can guarantee that there is a certain chain of links to follow that won't cause a cycle, and you can efficiently determine the 2d index of every node, you might have a chance.

Judge Mental
  • 5,209
  • 17
  • 22
  • Yeah :) I figured the complication hence i gave up trying to make a deep copy and just declaring then filling a new array. Even though i manage to deep copy the array, it will just as inefficient memory-wise as making a new one. Thanks anyway :) – JrL Aug 09 '12 at 10:32