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.
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:
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.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.