Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查二维阵列中的相邻图元并替换它们_Java_Arrays_Multidimensional Array - Fatal编程技术网

Java 检查二维阵列中的相邻图元并替换它们

Java 检查二维阵列中的相邻图元并替换它们,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,因此,我正在构建一种方法来检查2d数组的目标值,并用该目标值替换每个相邻元素。我已经花了大约一个小时的时间试图用头脑风暴来解决这个问题,我只想知道是否有人能帮我解决这个问题,这是我目前为止的代码 public int[][] replaceValue(int n, int[][]y){ int [][]temp0 = new int[y.length][y[0].length]; int[]top, down ,left, right = new int[y[0].length]; for(i

因此,我正在构建一种方法来检查2d数组的目标值,并用该目标值替换每个相邻元素。我已经花了大约一个小时的时间试图用头脑风暴来解决这个问题,我只想知道是否有人能帮我解决这个问题,这是我目前为止的代码

 public int[][] replaceValue(int n, int[][]y){
int [][]temp0 = new int[y.length][y[0].length];
int[]top, down ,left, right = new int[y[0].length];
for(int row = 0; row < y.length; row++){
  for(int col = 0; col < y[row].length; col++){
    temp0[row][col] = y[row][col];// new array so I wouldn't mess with the array passed in
  }
}
for(int row = 0; row < temp0.length; row++){
  for(int col = 0; col < temp0[row].length; col++){
    top[row] = temp0[row-1][col];
    down[row] = temp0[row+1][col];
    right[row] = temp0[row][col+1];
    left[row] = temp0[row] [col-1];
  }
}

我收到了一些错误消息,比如我没有初始化我的top、left、right和down变量,但我根本不理解检查相邻元素和确保整个数组没有被目标值替换的逻辑是如何工作的。谢谢你在第一次使用之前没有初始化你的局部变量。因此,您需要将第三行更改为以下代码:

int[] top = new int[temp[0].length], down = new int[temp[0].length], 
      left = new int[temp[0].length], right = new int[temp[0].length];

在编译代码之后,您可以检查您的逻辑。

这个问题有点让人困惑,所以我将尝试解释它

这里给出的是一个包含一些整数值的二维数组。你的函数应该扫描二维数组,如果你找到一些目标值, 返回一个二维数组,相邻索引也作为目标值

例如,如果我们有一个3x3数组,目标是2

1 1 1       1 2 1
1 2 1 ====> 2 2 2
1 1 1       1 2 1
您的问题是,在不将整个数组更改为2的情况下,无法想出更改值的方法

解决方案一:扫描给定数组中的目标值,但更新临时数组中的值

解决方案二:扫描临时数组,并使用二维布尔数组存储是否应该对其进行更改

解决方案1在内存和时间效率方面都要好得多,所以我只给你我的解决方案2,让你自己做解决方案1

另外,在重要的时候,请使用更具描述性的变量名:p为什么输入名为temp

public static int[][] replaceValue(int target, int[][] currArray){
        int[][] temp = new int[currArray.length][];

        //get a boolean array of same size
        //NOTE: it is initialized as false
        boolean[][] needsChange = new boolean[currArray.length][currArray[0].length];

        //copy the current array into temp
        for(int i = 0; i < currArray.length; i++){
            temp[i] = currArray[i].clone();
        }

        //Go through each value in the 2d array
        for(int i = 0; i < temp.length; i++){
            for(int j = 0; j < temp[0].length; j++){
                //if it is the target value, mark it to be changed
                if(temp[i][j] == target){
                    needsChange[i][j] = true;
                }
            }
        }

        //Go through each value in the 2d array
        for(int i = 0; i < temp.length; i++){
            for(int j = 0; j < temp[0].length; j++){
                if(needsChange[i][j]){ //NOTE: same as "needsChange[i][j] = true;"
                    //Now, we will check to make sure we don't go out of bounds
                    //Top
                    if(i > 0){
                        temp[i-1][j] = target;
                    }

                    //Bottom
                    if(i + 1 < temp.length){
                        temp[i+1][j] = target;
                    }

                    //Left
                    if(j > 0){
                        temp[i][j-1] = target;
                    }

                    //Right
                    if(j + 1 < temp[0].length){
                        temp[i][j+1] = target;
                    }
                }
            }
        }

        //return the new array we made
        return temp;
    }

你的方法还没有完全结束。请修改它以返回所需的值。我已经完成了它,但我的逻辑很愚蠢,我刚刚发布了第一部分,它告诉我需要初始化,对不起,我只是想理解它,我想它比这更复杂,我想我们需要存储每个的引用,这样我们就不会有所有内容都相同的错误,但会尝试这样做,谢谢,真正的解释这里真正的问题是我想知道它是如何工作的,但是谢谢,也许我的问题不是很清楚