在二维Java-Array中搜索和标记

时间:2014-11-14 08:58:45

标签: 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

这样做的最快方法是什么?

3 个答案:

答案 0 :(得分:0)

假设你使用二维数组:

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".
抱歉,这有点罗嗦,也许令人困惑,如果我需要澄清,请告诉我。

答案 1 :(得分:0)

到目前为止,您的代码在哪里?

您可以尝试使用嵌套的for循环,然后将if-else语句放在内部循环中
1。将查看索引左侧和右侧的值
2。如果它在2(并且是2)的旁边,将在索引中交换/分配2与a 1,否则它将不管它。

您是否希望将其设置为可扩展,以便它可以与0s,1s和2s的任何数组一起使用,或者只提供您提供的确切数组?

如果您需要任何说明并包含您的代码,请告知我们,以便我们能够解决问题。

答案 2 :(得分:0)

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

  1. 它生成带有随机数的二维数组:

    • array [] []是源数组,所有替换例程都将在被替换的[] []数组中执行(这是源代码的副本)。
    • SIZE_I - 行数和SIZE_J - 列数;
    • RANGE_FROM - 数组中的最小数字和RANGE_TO - 最大数字;
    • 搜索 - 要搜索的号码;
    • REPLACE - 要替换​​的号码。
    • 数量 - 数量或找到和替换的项目。
  2. 它将源数组打印到屏幕;

  3. 它检查源数组是否匹配SEARCH常量与数组项目数组[i] [j]。根据项目的位置,应该满足几个条件来替换被替换的[] []数组中的这个项目,并将数量变量增加一个。

  4. 它将已更换的数组打印到屏幕和已替换项目的数量。

  5. 我相信你可以通过将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();
    }