Home > Enterprise >  How to find cooresponding neighbor in 2D Array?
How to find cooresponding neighbor in 2D Array?

Time:12-01

I'm a beginner and I'm trying to figure out a way to get the corresponding neighbors of an index in a 2D array.


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));
    }
}

I created a simple 2D array above, and lets say I want to find the neighbors to index (2,2), so in this case given '13', I want to return the values '8', '18', '14, and '12'. I tried to use a nested for loop to get the values - 1 but I couldn't really figure it out.

CodePudding user response:

You can get the corresponding values by:

-finding what index your given value lays at

-returning it's -1 index and 1 index (warn: can cause nullpointer in some cases)

But remember that your array is only 5x4 in your mind, so the neighbours below and above are not determinable.

CodePudding user response:

It's not clear how you want to represent the neighbors in your 2D array return value, but here is how you access the neighboring cells.

There are fancy ways to write this in much less code, but I purposely wrote it out verbosely to illustrate the steps needed:

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!");
    }
}
  • Related