简体   繁体   English

读取二维数组的问题-Java

[英]Issues with reading a 2d Array - Java

I am trying to do Conway's game of life using 2d arrays. 我正在尝试使用2d数组进行Conway的生活游戏。 This method is supposed to look at every position on a 2d array and check each of its neighbors and count how many neighbors are surrounding the position (0 being empty and 1 being occupied). 该方法应该查看2d数组上的每个位置,并检查其每个邻居,并计算该位置周围有多少邻居(0为空,1被占用)。 It then performs some logic and decides if that position is dead or alive. 然后,它执行一些逻辑并确定该位置是死的还是活的。 The issue I am having is that by the time it checks the second position the values for the tempMatrix are wrong. 我遇到的问题是,当它检查第二个位置时,tempMatrix的值是错误的。 I have specifically been checking the first position [0][0] and it changes from 0 to 1 and I have no idea why. 我特别在检查第一个位置[0] [0],它从0变为1,我不知道为什么。 Thank you in advance for your help! 预先感谢您的帮助!

public static int[][] Evolve(int[][] _array){
    inputMatrix = _array;
    outputMatrix = inputMatrix;
    int [][] tempMatrix = inputMatrix;
    System.out.println("Output Matrix:");
    for (int x = 0; x < size; x++){
        for (int y = 0; y < size; y++){
            int neighbor_count = 0;
            ArrayList<int[]> neighbors = getNeighbors(x,y);
            for(int[] neighbor: neighbors){
                int tempX = neighbor[0];
                int tempY = neighbor[1];
                int temp = tempMatrix[tempX][tempY];
                if(temp == 1){
                    neighbor_count++;
                }
            }
            if(tempMatrix[x][y] == 1){
                if(neighbor_count == 1 || neighbor_count > 3) {
                    outputMatrix[x][y] = 0;
                }
                else{
                    outputMatrix[x][y] = 1;
                }
            }else if(neighbor_count == 3){
                outputMatrix[x][y] = 1;
            }else{
                outputMatrix[x][y] = 0;
            }
            System.out.printf("%2d ",outputMatrix[x][y]);
        }
        System.out.println();
    }
    return outputMatrix;
}

Your inputMatrix outputMatrix and tempMatrix are referring to the same 2D array. 您的inputMatrix outputMatrixtempMatrix引用相同的2D数组。 Therefore, when you modify the outputMatrix by the following code 因此,当您通过以下代码修改outputMatrix

if(tempMatrix[x][y] == 1){
            if(neighbor_count == 1 || neighbor_count > 3) {
                outputMatrix[x][y] = 0;
            }
            else{
                outputMatrix[x][y] = 1;
            }
        }else if(neighbor_count == 3){
            outputMatrix[x][y] = 1;
        }else{
            outputMatrix[x][y] = 0;
        }

The value of the tempMatrix also changes. tempMatrix的值也会更改。 Thus, try to make new 2D array for all the three matrices and then copy the value. 因此,尝试为所有三个矩阵创建新的2D数组,然后复制该值。

int inputMatrix[][]=new int[dimension][dimension];

Now copy the value of _array matrix to the inputMatrix . 现在,将_array matrix的值复制到inputMatrix

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

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