java 如何在二维数组中找到对应的邻居?

0yycz8jy  于 2022-12-02  发布在  Java
关注(0)|答案(3)|浏览(186)

我是一个初学者,我试图找出一种方法来获得一个二维数组中索引的对应邻居。

public class Main {

    public static int[][] graph(){
        int[][] myGraph = {
                {1,  2,  3,  4,  5},
                {6,  7,  8,  9,  10},
                {11, 12, 13, 14, 15},
                {16, 17, 18, 19, 20}
        };
        return myGraph;
    }

    public static int[][] findNeighbors(int[][] graph, int x, int y){
    
        for (int i = 0; i < graph.length; i++){
            for (int j = 0; j < graph[i].length; j++){
                
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(findNeighbors(graph(), 2, 2));
    }
}

我在上面创建了一个简单的二维数组,假设我想找到要索引的邻居(2,2),所以在这个例子中给定了'13',我想返回值'8','18','14和' 12 '。我试图使用嵌套的for循环来获得值+- 1,但我真的不能弄清楚。

kxxlusnw

kxxlusnw1#

您可以通过以下方式获取相应的值:

  • 查找给定值所在的索引
  • 返回其-1索引和+1索引(警告:在某些情况下可能导致空指针)
    但是请记住,在你的头脑中,你的数组只是5x 4,所以上下相邻的数组是不可确定的。
ffdz8vbo

ffdz8vbo2#

在二维数组返回值中如何表示相邻单元格尚不清楚,但下面介绍了访问相邻单元格的方法。
有很多奇特的方法可以用更少的代码来编写它,但我特意把它写得很冗长,以说明所需的步骤:

public static void findNeighbors(int[][] graph, int x, int y){
    int x2;
    int y2;
    int value;
    if (y>=0 && y<graph.length) {
        if (x>=0 && x<graph[0].length) {
            // North neighbor
            y2 = y - 1; 
            x2 = x;
            if (y2 >= 0) {
                value = graph[y2][x2];
                // ... do something with "value" ...
            }
            else {
                System.out.println("North is out of bounds!");
            }

            // East neighbor
            y2 = y; 
            x2 = x + 1;
            if (x2 < graph[y].length) {
                value = graph[y2][x2];
                // ... do something with "value" ...
            }
            else {
                System.out.println("East is out of bounds!");
            }

            // South neighbor
            y2 = y + 1; 
            x2 = x;
            if (y2 < graph.length) {
                value = graph[y2][x2];
                // ... do something with "value" ...
            }
            else {
                System.out.println("South is out of bounds!");
            }

            // West neighbor
            y2 = y; 
            x2 = x - 1;
            if (x2 >= 0) {
                value = graph[y2][x2];
                // ... do something with "value" ...
            }
            else {
                System.out.println("West is out of bounds!");
            }
        }
        else {
            System.out.println("x-coord out of bounds!");
        }
    }
    else {
        System.out.println("y-coord out of bounds!");
    }
}
ctehm74n

ctehm74n3#

public static List<Integer> findNeighbors(int[][] graph, int x, int y) {
    List<Integer> neighbors = new ArrayList<>();
    // Checking if x and y are inside the 2d Array if not return empty array
    if (y < 0 || y > graph().length - 1 || x < 0 || x > graph()[0].length - 1) return neighbors;
    // Checking if x - 1 is in bounds
    if (x - 1 >= 0) neighbors.add(graph[y][x - 1]);
    // Checking if x + 1 is in bounds
    if (x + 1 < graph()[0].length - 1) neighbors.add(graph[y][x + 1]);
    // Checking if y - 1 is in bounds
    if (y - 1 >= 0) neighbors.add(graph[y - 1][x]);
    // Checking if y + 1 is in bounds
    if (y + 1 < graph().length - 1) neighbors.add(graph[y + 1][x]);
    return neighbors;
}

相关问题