查找数组中元素周围的元素

md1*_*nox 15 java arrays multidimensional-array

我有一个多维数组,我想获取该数组中特定元素周围的元素.

例如,如果我有以下内容:

[[1,2,3,4,5,6]
 [8,9,7,5,2,6]
 [1,6,8,7,5,8]
 [2,7,9,5,4,3]
 [9,6,7,5,2,1]
 [4,7,5,2,1,3]]
Run Code Online (Sandbox Code Playgroud)

如何查找上述任何元素周围的所有8个元素?我如何处理边缘的元素?

我想到的一种方法是,为此编写一个9行代码,这是显而易见的,但是有更好的解决方案吗?

zve*_*vez 9

您可以在表单中使用'direction array'

[[-1,-1], [-1,0],[1,0]..and so on]
Run Code Online (Sandbox Code Playgroud)

并且采用点坐标并通过方向数组迭代的方法 - >向坐标添加方向数,检查索引不超出范围并收集结果.像这样的东西:

private static int[][] directions = new int[][]{{-1,-1}, {-1,0}, {-1,1},  {0,1}, {1,1},  {1,0},  {1,-1},  {0, -1}};

static List<Integer> getSurroundings(int[][] matrix, int x, int y){
    List<Integer> res = new ArrayList<Integer>();
    for (int[] direction : directions) {
        int cx = x + direction[0];
        int cy = y + direction[1];
        if(cy >=0 && cy < matrix.length)
            if(cx >= 0 && cx < matrix[cy].length)
                res.add(matrix[cy][cx]);
    }
    return res;
}
Run Code Online (Sandbox Code Playgroud)


Roh*_*ain 5

对于 (i, j) ->

              (i - 1, j - 1)
              (i - 1, j)
              (i - 1, j + 1)

              (i, j - 1)
              (i, j + 1)

              (i + 1, j - 1)
              (i + 1, j)
              (i + 1, j + 1)
Run Code Online (Sandbox Code Playgroud)

现在,在边缘,您可以检查num % row == 0,然后是在行边缘...,num % col == 0然后是在列边缘。

以下是您可以继续的方法:-

给定一个索引(i, j)。您可以在与jfor i - 1、 theni和 then相邻的行中找到元素i + 1。(注意: - 对于索引,i您只需要访问j - 1, 和j + 1

随后,您还可以检查row edgecolumn edge..

在这里,您可以查看下面的代码,了解它是如何发生的:-

    // Array size
    int row = 6;
    int col = 6;
    // Indices of concern
    int i = 4;
    int j = 5;

    // To the left of current Column
    int index = i - 1;
    for (int k = -1; k < 2; k++) {
        if (index % row > 0 && ((j + k)  % col) > 0) {
            System.out.println(arr[index][j + k]);
        }
    }


    // In the current Column
    index = i;

    // Increment is 2 as we don't want (i, j)
    for (int k = -1; k < 2; k = k + 2) {            
        if (index % row > 0 && ((j + k)  % col) > 0) {
            System.out.println(arr[index][j + k]);
        }
    }

    // To the right of current Column
    index = i + 1;
    for (int k = -1; k < 2; k++) {
        if (index % row > 0 && ((j + k)  % col) > 0) {
            System.out.println(arr[index][j + k]);
        }

    }
Run Code Online (Sandbox Code Playgroud)

更新: - 上面的代码可以进一步简化.. 但我把这个任务留给你.. 提示: - 你可以从那里减少一个 for 循环..

  • @RohitJain 我不明白你对我的评论。对 vineetrok 的评论是不正确的。您不必事先检查它。您可以在访问中使用模数。看我之前的评论。 (2认同)

md1*_*nox 2

for (i = 0; i < array.length; i++) {
            for (j = 0; j < array[i].length; j++) {
                for (x = Math.max(0, i - 1); x <= Math.min(i + 1, array.length); x++) {
                    for (y = Math.max(0, j - 1); y <= Math.min(j + 1,
                            array[i].length); y++) {
                        if (x >= 0 && y >= 0 && x < array.length
                                && y < array[i].length) {
                            if(x!=i || y!=j){
                            System.out.print(array[x][y] + " ");
                            }
                        }
                    }
                }
                System.out.println("\n");
            }
        }
Run Code Online (Sandbox Code Playgroud)

感谢所有回答的人,但我在刚刚发现的这篇文章的帮助下找到了答案,上面是解决方案。再次感谢 :)