简体   繁体   中英

Java: find in 2d array all adjacent elements with the same value, starting from a given element

I'm working on a program which contains a 2-dimensional 16x32 char array. What I want to do is, starting from a given element in this array, find all the elements that share the same value (in my case a blank space ' ') and that are horizontally and/or vertically linked to each other.

The method I'm using stores the indexes that it finds inside another array, called toShow ( public static int toShow[][] = new int[30][30]; ). The problem is that this method does not seem to process towards the right side. Strangely enough, it seems to work on the other sides... Here's an example:

X1    123
31     1X
211    24
1X1  112X
111 12X34
111•2X32X
1X113X211

In this case, starting from the element marked as •, the method should store every ' ' character and all the neighbor numbers... but this is the result:

1••
1••
 1•
 1•
 1•
 1•

It does however usually work if it starts in the lower left corner, even though it does have to turn right!

I don't understand what's wrong with my code... Anyways here is the odd method:

public static void getEmptySurrounding(int xcoord, int ycoord) {
    if (toShow[xcoord][ycoord] == 1) {
        return;
    }

    else {
        toShow[xcoord][ycoord] = 1;
    }

    //DOWN
    if((ycoord!=29) && ycoord + 1 < 16) {
        if (board[xcoord][ycoord] == ' ') {
            getEmptySurrounding(xcoord, ycoord + 1);
        }
    }
    //RIGHT
    if((xcoord!=15) && xcoord + 1 < 30) {
        if (board[xcoord][ycoord] == ' ') {
            getEmptySurrounding(xcoord + 1, ycoord);
        }
    }
    //UP
    if((ycoord!=0) && ycoord - 1 >= 0) {
        if (board[xcoord][ycoord] == ' ') {
            getEmptySurrounding(xcoord, ycoord - 1);
        }
    }
    //LEFT
    if((xcoord!=0) && xcoord - 1 >= 0) {
        if (board[xcoord][ycoord] == ' ') {
            getEmptySurrounding(xcoord - 1, ycoord);
        }
    }   
}

Thank you!

Based on the information you provided I made an application to test your method:

public class Mine {

    private static char board[][] = new char[16][32];
    private static int toShow[][] = new int[30][30];

    public static void main(String[] args) {
        int n = 0;
        insert(n++, "X1    123");
        insert(n++, "31     1X");
        insert(n++, "211    24");
        insert(n++, "1X1  112X");
        insert(n++, "111 12X34");
        insert(n++, "111 2X32X");
        insert(n++, "1X113X211");

        getEmptySurrounding(3, 5);

        for (int i = 0; i < 30; i++) {
            for (int j = 0; j < 30; j++) {
                System.out.print(toShow[j][i]);
            }
            System.out.println();
        }
    }

    public static void getEmptySurrounding(int xcoord, int ycoord) {
        if (toShow[xcoord][ycoord] == 1) {
            return;
        }

        else {
            toShow[xcoord][ycoord] = 1;
        }

        // DOWN
        if ((ycoord != 29) && ((ycoord + 1) < 16)) {
            if (board[xcoord][ycoord] == ' ') {
                getEmptySurrounding(xcoord, ycoord + 1);
            }
        }
        // RIGHT
        if ((xcoord != 15) && ((xcoord + 1) < 30)) {
            if (board[xcoord][ycoord] == ' ') {
                getEmptySurrounding(xcoord + 1, ycoord);
            }
        }
        // UP
        if ((ycoord != 0) && ((ycoord - 1) >= 0)) {
            if (board[xcoord][ycoord] == ' ') {
                getEmptySurrounding(xcoord, ycoord - 1);
            }
        }
        // LEFT
        if ((xcoord != 0) && ((xcoord - 1) >= 0)) {
            if (board[xcoord][ycoord] == ' ') {
                getEmptySurrounding(xcoord - 1, ycoord);
            }
        }
    }

    public static void insert(int n, String a) {
        for (int i = 0; i < 16; i++) {
            board[i][n] = a.length() <= i ? ' ' : a.charAt(i);
        }
    }

}

At the end it prints out the content of toShow , the relevant part is:

011111100000000000000000000000
011111110000000000000000000000
001111110000000000000000000000
001111100000000000000000000000
001110000000000000000000000000
001110000000000000000000000000
000100000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000
000000000000000000000000000000

which suggest that the method works correctly.

So probably the problem lies elsewhere in your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM