-1

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.

Natchy
  • 9
  • 1
    think you already have the initial x and y and you want only the other 4 so you want x + 1 and x - 1 and y + 1 and y - 1 and do you really need to return an 2d array or you only need a simple array – Anon Nov 30 '22 at 21:32
  • Does [finding the neighbors of 2d array](https://stackoverflow.com/questions/43816484/finding-the-neighbors-of-2d-array) solve your problem? The logic is similar. – MT756 Nov 30 '22 at 21:54
  • You're return type is a 2D array. What SIZE were you thinking, and what would be the LAYOUT of the neighboring values and/or the original value in this 2D array? – Idle_Mind Nov 30 '22 at 23:26

3 Answers3

0

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.

Windir
  • 98
  • 7
0

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!");
    }
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0
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;
}
Anon
  • 385
  • 2
  • 6