简体   繁体   English

比较2D数组Java

[英]Comparing 2D arrays Java

I'm trying to find out where the second array given will positioned in the first array but cant seem to convert then to string to find out where they will be: 我试图找出给定的第二个数组将在第一个数组中放置的位置,但是似乎无法转换为字符串以找出它们的位置:

public static void main(String[] args) {
    char i [][] = new char[][]{
            {'d', 's', 'l', 'e', 'i', 'g', 'h', 'e', 'i', 'j', 'a', 's', 'l', 'd', 'k', 'j'},
            {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'W', 'w', 'Z', 'Z', 'Z', 'W', '1', 'l', 'k'},
            {'h', 'i', 'j', 'k', 'l', 'm', 'n', 'Z', 'A', 'a', 'Z', 'a', 'Z', '2', 'i', 'n'},
            {'o', 'p', 'q', 'r', 's', 't', 'u', 'Z', 'Z', 'L', 'l', 'Z', 'Z', '3', 'i', 'v'},
            {'v', 'w', 'x', 'y', 'z', '1', '2', 'Z', 'd', 'Z', 'D', 'd', 'Z', '4', 'q', 'i'},
            {'3', '4', '5', '6', '7', '8', '9', 'o', 'Z', 'Z', 'o', 'O', 'Z', '5', 'b', 'v'},
            {'k', 'e', '8', '7', '8', '4', 'j', 'f', 'l', 'k', 'a', 'v', '8', '8', 'h', 'j'}
    };

    char w [][] = new char[][]{
            {'W', 'w', '.', '.', '.', 'W', '1'},
            {'.', 'A', 'a', '.', 'a', '.', '2'},
            {'.', '.', 'L', 'l', '.', '.', '3'},
            {'.', 'd', '.', 'D', 'd', '.', '4'},
            {'o', '.', '.', 'o', 'O', '.', '5'}
    };

    find(i, w);
}

    public static int[] find(char [][] image, char [][] waldo) {
    for (int i = 0; i < waldo.length; i++) {
        char[] largerCharArray= large[i];
        String largerString = new String(largerCharArray);

        //used for debug purposes
        char[] array = largerCharArray;

        char [] smallCharArray = small[i];
        String smallString = new String(smallCharArray);

        char[] array1 = smallCharArray;

        //beginning comparison
        if (largerString.indexOf(smallString) >= 0) {

           int temp = largerString.indexOf(smallString);
        }

        //debug purposes
        System.out.println(largerString.indexOf(smallString));
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array1));
    }
    //for debug purposes
    return null;
}

As I understand the task is to find a matching submatrix. 据我了解,任务是找到一个匹配的子矩阵。 If you replace dots with wildcards, it makes sense. 如果将点替换为通配符,则很有意义。 So the smaller matrix is at the position (row = 2, column = 8) counting from 1. 因此,较小的矩阵位于从1开始计数的位置(行= 2,列= 8)。

Here is the code which searches for submatrix. 这是搜索子矩阵的代码。 I used regular expressions to match rows as strings: 我使用正则表达式将行匹配为字符串:

public static int[] find(char [][] large, char [][] small) {
    outer: for (int i = 0; i < large.length - small.length; i++) {

        int column = -1;

        for (int j = 0; j < small.length ; j++) {
            String largerString = new String(large[i + j]);

            String smallString = new String(small[j]);

            Matcher matcher = Pattern.compile(smallString).matcher(largerString);

            if(!matcher.find()){
                continue outer;
            }

            int thisColumn = matcher.start();

            if(column != -1 && column != thisColumn){
                continue outer;
            }

            column = thisColumn;
        }

        System.out.printf("found it: %d, %d!", i, column);
    }

    //for debug purposes
    return null;
}

Try this code I've just finished, it may have some bugs, it will try to improve it. 试试我刚刚完成的这段代码,它可能会有一些错误,它将尝试改进它。 It doesn't use Strings. 它不使用字符串。

private static int[] find(char[][] image, char[][] waldo) {
    int row = -1;
    int column = -1;
    char first_waldo = waldo[0][0]; // first char of waldo
    boolean continue1 = true; // this is used to avoid looping when the indexes tried are not a possible answer
    int size = waldo.length * waldo[0].length; // number of elements of waldo
    int cont = 0;

    for (int i = 0; i < image.length; i++) {
        for (int j = 0; j < image[i].length; j++) { // Looping over chars in image array
            char current = image[i][j]; // Current char of image array
            if (current == first_waldo) { // If true, this indexes (i, j) are a possible answer
                row = i; // Current image row
                column = j; // Current image column
                cont = 0; // Count of how many chars are matched
                for (int k = row; k < row + waldo.length; k++) {
                    for (int l = column; l < column + waldo[0].length; l++) { // Looping over
                                                       // the possible matching array in image
//                          System.out.println("Comparing " + image[k][l] + " with " + waldo[k - row][l - column]);
//                          System.out.println(k + " " + l);
                        if (waldo[k - row][l - column] == '.') { // If its a point, count as matching characters
                            cont++;
                            continue; 
                        }
                        if (image[k][l] != waldo[k - row][l - column]) { // If chars are different, it's not the answer
                            row = -1;
                            column = -1;
                            continue1 = false; // So it doesn't continue looping
                            break;
                        } else {
                            cont++; // If they are equals, count as matching characters
                        }
                    }
                    if (continue1 == false) { // To avoid overlooping when it's not the answer
                        continue1 = true; // Reset value
                        break;
                    }
                }
                if (cont == size) { // Is the number of matched charactes equal to the size of waldo?
                    int[] res = {row, column};
                    return res; // Return indexes
                }

            } else {
                row = -1;
                column = -1;
            }

        }
    }
    int[] res = {-1, -1};
    //      System.out.println("\n" + row +" , " +  column);
    return res; // Not answer

}

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

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