简体   繁体   English

Java:从给定元素开始,在2D数组中查找具有相同值的所有相邻元素

[英]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. 我正在开发一个包含二维16x32 char数组的程序。 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]; ). 我使用的方法将在另一个数组中找到的索引存储在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: 最后,它打印出toShow的内容,相关部分是:

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. 因此,问题可能出在程序的其他地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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