简体   繁体   English

深拷贝2维数组

[英]Deep Copy 2 Dimensional Array

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 ? 我的问题是如何将OriginalArrayOfNode的深层副本复制到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. 我试图用4个指针为Knuth的Dancing Link算法制作一个圆形双链表的副本。 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. 很难追踪问题的位置,但我假设如果原始数组由于Knuth的DL算法而给出“x”,那么原始数组的正确深度副本也会给出“x”作为结果,假设没有其他变量且没有随机修饰符。 However, ive tried clone() method, arrayutil.copy() method, and none of them give a "correct" deep copy based on my assumption above. 但是,我已经尝试了clone()方法,arrayutil.copy()方法,并且根据我上面的假设,它们都没有给出“正确的”深层复制。

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. 注意:当您从0复制到其他复制时,应该从1开始复制。

What I would suggest you do however, is add into your class node a clone() method. 但是我建议你做的是在你的类节点中添加一个clone()方法。 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(...) . 最后,需要注意的另一点是,如果您尝试以您正在进行的方式进行深度复制,则可以使用ArrayUtil.copy(...)轻松地从索引1 - 200填充。

Hope all this helps. 希望这一切都有帮助。

I presume the OriginalArrayOfNode contains nodes that refer to other nodes in the same array? 我假设OriginalArrayOfNode包含引用同一数组中其他节点的节点? 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. 在这种情况下,您无法进行深层复制,除非您加强node数据结构以包含其自己的2d索引。 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. 例如,如果OriginalArrayOfNode[0][0].right碰巧引用OriginalArrayOfNode[15][27] ,你将无法弄清楚你需要做一个索引的深层复制[15][27]除非您使用对象标识彻底搜索旧数组,否则在将结果分配给CopyArrayOfNode[0][0].right之前,从旧数组到新数组。

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. 即使你可以忍受搜索所有节点的强力搜索,或者你可以修改node数据结构以包含它自己的索引,也可能会有跟随这些链接形成的循环,这使得任何确定正确顺序的尝试都变得非常复杂复制东西。 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. 如果您可以保证有一个链接链接不会导致循环,并且您可以有效地确定每个节点的2d索引,那么您可能有机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM