有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在2d矩阵的子矩阵中找到重复项并进行比较

我有个问题。有人能帮我在子矩阵中找到重复的吗? 我有一个在2d矩阵中找到子矩阵的代码,但我找不到重复的。我想把这些值推到堆栈上(因为在赋值中我应该使用堆栈),找到每个子矩阵中的所有重复项,然后比较它们,但我真的不知道怎么做。如果有人帮我完成这个项目,我将非常感激

我的代码:

public static void main(String[] args) throws Exception {
        int[][] data = new int[3][3];
        Random random = new Random();
        for(int i=0; i<data.length;i++)
        {
            for(int j=0; j<data.length; j++)
            {
                data[i][j] = random.nextInt(10);
            }
        }
        printSubMatrix(data);
    }

private static void printSubMatrix(int[][] mat) {
        int rows=mat.length;
        int cols=mat[0].length;
        Stack _stack = new Stack();
        //prints all submatrix greater than or equal to 2x2
        for (int subRow = rows; subRow >= 2; subRow--) {
            int rowLimit = rows - subRow + 1;
            for (int subCol = cols; subCol >= 2; subCol--) {
                int colLimit = cols - subCol + 1;
                for (int startRow = 0; startRow < rowLimit; startRow++) {
                    for (int startCol = 0; startCol < colLimit; startCol++) {
                        for (int i = 0; i < subRow; i++) {
                            for (int j = 0; j < subCol; j++) {
                                System.out.print(mat[i + startRow][j + startCol] + " ");
                                _stack.push(mat[i+startRow][j+startCol]);
                            }
                            System.out.print("\n");
                        }
                        System.out.print("\n");
                    }
                }
            }
        }
    System.out.printf(_stack.toString().replaceAll("\\[", "").replaceAll("]", ""));
    }

共 (0) 个答案