为什么我的2D阵列不止一次克隆? [英] Why is my 2D array cloning more than once?

查看:38
本文介绍了为什么我的2D阵列不止一次克隆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想将'线索'数组复制到'木板'数组一次 .复制一次后,线索数组为什么会随板一起变化?

I want to copy the 'clues' array to the 'board' array only once. Why does the clues array change along with board after copying once?

public class Ejewbo
{
    public static int[][] board = new int[9][9];
    public static int[][] clues = 
        {
            {0, 0, 0, 7, 0, 0, 0, 0, 0},
            {1, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 4, 3, 0, 2, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 6},
            {0, 0, 0, 5, 0, 9, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 4, 1, 8},
            {0, 0, 0, 0, 8, 1, 0, 0, 0},
            {0, 0, 2, 0, 0, 0, 0, 5, 0},
            {0, 4, 0, 0, 0, 0, 3, 0, 0},
        };

    public static void main(String[] args)
    {
        Ejewbo.board = Ejewbo.clues.clone();
        test();
    }

    public static void printboth()
    {
        for (int j = 0; j < 9; j++)
        {
            for (int i = 0; i < 9; i++)
            {
                System.out.print(Ejewbo.board[j][i]);
                System.out.print(" ");
            }

            System.out.println();
        }

        System.out.println();

        for (int j = 0; j < 9; j++)
        {
            for (int i = 0; i < 9; i++)
            {
                System.out.print(Ejewbo.clues[j][i]);
                System.out.print(" ");
            }

            System.out.println();
        }

        System.out.println("-----");
    }

    public static void test()
    {
        for (int i = 0; i < 2; i++) //run twice to see issue
        {
            Ejewbo.board[0][0]++;
            printboth();
        }
    }
}

我希望线索数组不会改变,但是确实会改变.董事会变更后,线索也随之改变.为什么?有没有更好的方法来复制像这样的数组(而不是使用.clone())?

I would expect the clues array not to change, but it does. When a change is made to board, clues changes too. Why? Is there a better way to copy arrays like this (instead of using .clone())?

此处的第一个答案似乎是解决问题的一种好方法我要复制我的数组.

The first answer here seems to be a good way for me to copy my arrays.

推荐答案

(以下代码块未经测试)

(code blocks below are untested)

致电时:

Ejewbo.board = Ejewbo.clues.clone();    

您正在创建浅表副本.结果是您观察到的行为,board [i] [j] =线索[i] [j].他们指向的是内存中相同的引用,因此对一个引用的任何更改也将对另一个引用进行更改.

You are creating a shallow copy. The result is the behaviour that you have observed, board[i][j] = clues[i][j]. They are pointing to the same reference in memory and therefore any changes to one is also a change to the other.

相反,您可以做的是使用类似的方法在2D数组上进行迭代

What you could do instead is iterate over the 2D array itself with something like

for(int i=0; i<clues.length; i++) {
 for(int j=0; j<clues[i].length; j++) {
  board[i][j]=clues[i][j];
 }
}

请注意,Arrays类具有一种用于复制一维数组内容的方法,称为copyOf.因此,以上代码可以简化为

Note, the Arrays class has a method for copying the contents of a one dimensional array over, known as copyOf. So the above code could be shortened to

for(int i=0; i<clues.length; i++) {
 board[i]=Arrays.copyOf(clues[i], board[i].length);
}

请注意,如果Arrays.copyOf方法的长度不匹配,则会截断多余的元素或pad元素(即为int数组添加0),但是您可能会确保这样做.

Note that the Arrays.copyOf method will truncate extra elements or pad elements (i.e will add 0s for your int array) if they do not match in length, however you likely will ensure that they do so.

这篇关于为什么我的2D阵列不止一次克隆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆