简体   繁体   English

Java在2D数组中更改值

[英]Java changing values in a 2D array

I'm working with a 2D array and what I'm trying to do in the below method is swap two values. 我正在使用2D数组,在以下方法中尝试执行的操作是交换两个值。 The 'currentBoard' variable is a 2D array that needs should not be edited. “ currentBoard”变量是一个2D数组,不应编辑该数组。 The 'cpy' variable a duplicate of the 'currentBoard' and needs its variables to be changed. “ cpy”变量是“ currentBoard”的副本,需要更改其变量。 'nbt' and 'bt' are 1D arrays that used to point to an index in the 2D array. 'nbt'和'bt'是一维数组,用于指向2D数组中的索引。 The code for the function 'copyBoard' is always below if it helps 如果有帮助,函数“ copyBoard”的代码始终在下面

The issue I'm having is that at on the line marked with ** when the value in the 'cpy' array is changed for some reason the value in 'currentBoard' is being changed as well. 我遇到的问题是,当“ cpy”数组中的值由于某种原因而更改时,在标有**的行上也更改了“ currentBoard”中的值。 I really can't figure out why this is happening.... 我真的不知道为什么会这样。


private void Swap(int[] nbt, int[] bt, ArrayList<State> children, String direction) {

    int[][] cpy = copyBoard(currentBoard);
    int temp = cpy[nbt[0]][nbt[1]];
    **cpy[nbt[0]][nbt[1]] = currentBoard[bt[0]][bt[1]];
    cpy[bt[0]][bt[1]] = temp;
    children.add(new Board(cpy, this.getGOAL(), this.getRows(), this.getColumns(), (this.getDirections() + direction + ", ")));
}

In case it helps here is the values that are assigned to the variables at the point when the code is on the line marked with ** 如果这有帮助,那么在代码位于标有**的行上时,分配给变量的值会有所帮助


nbt = {1, 0} bt = {0, 0} nbt = {1,0} bt = {0,0}

private int[][] copyBoard(int[][] state)
{
    int[][] returnArray = new int[rows][columns];
    for (int i = 0, j = 0; i*j < PUZZLE_SIZE; i++, j++)
    {
        returnArray[i] = state[i];
    }
    return returnArray;
}

A 2D array is an array of references to arrays. 2D数组是对数组的引用的数组。 So if you assign returnArray[i] = state[i] , you are simply making returnArray[i] refer to the same array that state[i] refers to. 因此,如果你分配returnArray[i] = state[i]时,只是使returnArray[i]是指相同的阵列state[i]是指。 Thus, modifying returnArray[i][j] will modify the j'th element of whatever state[i] was. 因此,修改returnArray[i][j]将修改任何state[i]第j个元素。 You have to create a deep copy of the "rows", too, eg: 您还必须创建“行”的深层副本,例如:

private int[][] copyBoard(int[][] state)
{
    int[][] returnArray = new int[rows][columns];
    for (int i = 0, j = 0; i*j < PUZZLE_SIZE; i++, j++)
    {
        // deep copy of row:
        returnArray[i] = Arrays.copyOf(state[i], state[i].length);
    }
    return returnArray;
}

Check out this short write-up on 2D arrays , it should give you a better idea of what's going on here. 查看有关2D数组的简短文章 ,它应该使您对这里发生的事情有一个更好的了解。 In particular, this little image, which represents int nums[][] = new int[5][4] (although it makes more sense in context): 特别是,这个小图像代表int nums[][] = new int[5][4] (尽管在上下文中更有意义):

在此处输入图片说明


By the way, your loop logic looks a little odd to me; 顺便说一下,您的循环逻辑对我来说有点奇怪; even if the math happens to work out in your situation, it is a bit clearer to do this instead: 即使在您遇到的情况下数学运算成功,也可以这样做:

for (int i = 0; i < rows; i++)

Or more generally: 或更笼统地说:

for (int i = 0; i < returnArray.length; i++)

These, of course, assume that state.length == rows ; 这些当然假设state.length == rows ; but you get the idea. 但你明白了。

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

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