Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.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_Search - Fatal编程技术网

二维Java数组中的搜索和标记

二维Java数组中的搜索和标记,java,arrays,search,Java,Arrays,Search,我有一个Java的二维数组。这些值如下所示: 2 1 0 2 1 1 0 2 2 2 1 1 1 2 2 0 0 0 2 2 我现在想把所有的“2”都改成1,但只有那些与其他的相邻的。因此,在说“将(4 | 4)更改为”2“之后,我希望我的数组如下所示: 2 1 0 1 1 1 0 1 1 1 1 1 1 1 1 0 0 0 1 1 最快的方法是什么?假设您使用二维阵列: have int x and y set to 0 start from [x][y] on the array S

我有一个Java的二维数组。这些值如下所示:

2 1 0 2 1 
1 0 2 2 2
1 1 1 2 2
0 0 0 2 2
我现在想把所有的“2”都改成1,但只有那些与其他的相邻的。因此,在说“将(4 | 4)更改为”2“之后,我希望我的数组如下所示:

2 1 0 1 1 
1 0 1 1 1
1 1 1 1 1
0 0 0 1 1

最快的方法是什么?

假设您使用二维阵列:

have int x and y set to 0
start from [x][y] on the array
Step 1: Check to if number equals "2"
  If it does and:
  0<x<4 and 0<y<4: Check [x-1][y-1],[x][y-1],[x][y+1],[x-1][y],[x+1][y], and [x+1][y+1]
  x=0 and 0<y<3: Check [x][y-1],[x][y+1],[x+1][y], and [x+1][y+1]
  x=4 and 0<y<3: Check [x-1][y-1],[x][y-1],[x][y+1],[x-1][y]
  0<x<4 and y=0: Check [x][y+1],[x-1][y],[x+1][y], and [x+1][y+1]
  0<x<4 and y=3: Check [x-1][y-1],[x][y-1],[x-1][y],[x+1][y]
  and so on....
check to see if [x][y] = any of the checks
   if so: store a 1 in [x][y] in an alternative array of the same size (lets call it flags)
iterate and repeat from step 1 until you have gone through the entire array
run through the flags and if the value is 1 at any address [x][y] change the corresponding value in our original array to "1".
将int x和y设置为0
从阵列上的[x][y]开始
步骤1:检查数字是否等于“2”
若有,及

0到目前为止您尝试的代码在哪里

您可以尝试使用嵌套的for循环,然后将if-else语句放入
1.将查看索引左侧和右侧的值
2.如果它紧挨着a 2(并且是a 2),将交换/分配索引w/a 1中的2,否则它将不处理它

您是否希望使其具有可扩展性,以便它可以与0、1和2的任何阵列一起工作,或者仅与您提供的这个阵列一起工作


如果您需要任何澄清,请告诉我,并包括您的代码,以便我们能够解决一些问题。

此示例使用2D数组作为数组数组数组

  • 它使用随机数生成二维数组:

    • 阵列[][]是源阵列,所有替换例程都将在替换的[][]阵列(源阵列的副本)中执行
    • SIZE_I-行数,SIZE_J-列数
    • RANGE_FROM-数组中的最小数,RANGE_TO-最大数
    • 搜索-要搜索的编号
    • REPLACE—要替换的编号
    • 数量-找到和替换的项目的数量
  • 它将源阵列打印到屏幕上

  • 它检查源数组是否与数组项数组[i][j]匹配搜索常量。根据项的位置,应满足多个条件才能在替换的[][]数组中替换此项,并将数量变量增加一个

  • 它将替换的数组打印到屏幕上,并显示替换的项目数

  • 我相信您可以通过将2D阵列作为一个阵列来获得性能。 例如,2D数组可以表示为{2,1,0,2,1,1,0,2,2,2,1,1,1,2,2,0,0,0,2,2},但您应该正确处理它

    public static void main(String[] args) {
    final int RANGE_FROM = 0;
    final int RANGE_TO = 2;
    final int SEARCH = 2;
    final int REPLACE_TO = 1;
    final int SIZE_I = 4;
    final int SIZE_J = 5;
    int quantity = 0;
    int array[][] = new int[SIZE_I][SIZE_J];
    int replaced[][] = new int[SIZE_I][SIZE_J];
    
    // Generate arrays
    for (int i = 0; i < SIZE_I; i++) {
        for (int j = 0; j < SIZE_J; j++) {
        array[i][j] = (int) (RANGE_FROM + Math.random() * (RANGE_TO - RANGE_FROM + 1));
        replaced[i][j] = array[i][j];
        }
    }
    
    // Display array
    System.out.println("Source array:");
    for (int x[]: array) {
        for (int y: x) {
        System.out.print(y + " ");
        }
        System.out.println();
    }
    System.out.println();
    
    // Check array
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array[i].length; j++) {
        if (i == 0 && j == 0) {
            if (array[i][j] == SEARCH && (array[i+1][j] == SEARCH || array[i][j+1] == SEARCH)) {
            quantity++;
            replaced[i][j] = REPLACE_TO;
            }
        }
        else if (i == 0 && j == array[i].length - 1) {
            if (array[i][j] == SEARCH && (array[i+1][j] == SEARCH || array[i][j-1] == SEARCH)) {
            quantity++;
            replaced[i][j] = REPLACE_TO;
            }
        }
        else if (i == array.length -1 && j == 0) {
            if (array[i][j] == SEARCH && (array[i-1][j] == SEARCH || array[i][j+1] == SEARCH)) {
            quantity++;
            replaced[i][j] = REPLACE_TO;
            }
        }
        else if (i == array.length -1 && j == array[i].length - 1) {
            if (array[i][j] == SEARCH && (array[i][j-1] == SEARCH || array[i-1][j] == SEARCH)) {
            quantity++;
            replaced[i][j] = REPLACE_TO;
            }
        }
        else if (i == 0 && j != 0 && j != array[i].length - 1) {
            if (array[i][j] == SEARCH && (array[i][j-1] == SEARCH || array[i+1][j] == SEARCH || array[i][j+1] == SEARCH)) {
            quantity++;
            replaced[i][j] = REPLACE_TO;
            }
        }
        else if (i != 0 && i != array.length - 1 && j == array[i].length - 1) {
            if (array[i][j] == SEARCH && (array[i][j-1] == SEARCH || array[i-1][j] == SEARCH || array[i+1][j] == SEARCH)) {
            quantity++;
            replaced[i][j] = REPLACE_TO;
            }
        }
        else if (i == array.length - 1 && j != 0 && j != array[i].length - 1) {
            if (array[i][j] == SEARCH && (array[i][j-1] == SEARCH || array[i-1][j] == SEARCH || array[i][j+1] == SEARCH)) {
            quantity++;
            replaced[i][j] = REPLACE_TO;
            }
        }
        else if (i != 0 && i != array.length - 1 && j == 0) {
            if (array[i][j] == SEARCH && (array[i-1][j] == SEARCH || array[i][j+1] == SEARCH || array[i+1][j] == SEARCH)) {
            quantity++;
            replaced[i][j] = REPLACE_TO;
            }
        }
        else {
            if (array[i][j] == SEARCH && (array[i-1][j] == SEARCH || array[i][j+1] == SEARCH || array[i+1][j] == SEARCH || array[i][j-1] == SEARCH)) {
            quantity++;
            replaced[i][j] = REPLACE_TO;
            }
        }
        }       
    }
    
    // Display replaced array
    System.out.println("Replaced array:");
    for (int x[]: replaced) {
        for (int y: x) {
        System.out.print(y + " ");
        }
        System.out.println();
    }
    System.out.println();
    System.out.println("Replace quantity: " + quantity);
    System.out.println();
    }
    
    publicstaticvoidmain(字符串[]args){
    最终整数范围_FROM=0;
    最终整数范围_至=2;
    最终整数搜索=2;
    最终int替换_至=1;
    最终整数大小_I=4;
    最终整数大小_J=5;
    整数数量=0;
    int数组[][]=新int[SIZE_I][SIZE_J];
    替换的整数[][]=新整数[SIZE_I][SIZE_J];
    //生成数组
    对于(int i=0;i
    您在我的示例中看到的“与其他2相邻”是什么意思?2 at(0 | 0)不应更改。只应更改其他2旁边的2。因此,如果a 2与另一个2共享一个“边”,您可以更改它,请详细说明您的答案,以便清楚您的代码是什么?