【问题标题】:How to check if an matrix (array) has a column that it's the same than a row如何检查矩阵(数组)是否具有与行相同的列
【发布时间】:2016-04-08 01:29:12
【问题描述】:

我已经在一个年级学习了三个月的 Java,现在我正在重做一些我们以前做过的练习,而我正在做一个以前做不到的练习。 措辞是这样的: 编写一个程序,通过扫描仪启动一个 NxN 矩阵,并判断是否存在与某列完全相同的行,并连接相同的行和列的索引。 例如,如果我们有下一个二维数组:

   0   1   2
0 [1] [2] [1]
1 [4] [8] [2]
2 [3] [5] [1]

程序会告诉我第 0 行与第 2 列相同。 对不起我的英语不好,我是西班牙人。 所以这是我做的代码:

package Arrays;

public class Ejercicio16 {



    public static void main(String[] args) {
            int matrix[][];
            int i, j;
            int rows, columns;
            boolean same = false;

            //1.Ask inf of the matrix to the user.
            do{ 
                rows = Teclado.nextInt("Insert the number of rows of the matrix");
                columns = Teclado.nextInt("Insert the number of columns of the matrix");    
            }while(rows<=0 && columns<=0);

    /*Teclado = A java class that I use with methos like nextInt, nextByte, etc with their try catchs and so on.It would be a Scanner method*/


    matrix = new int[rows][columns];



    for (i=0; i<rows; i++) {
        for (j=0; j<columns; j++){
            matrix[i][j] = Teclado.nextInt("Insert a value for the cell of the row: "+ i + " column: " + j);
                        }
                    }

            //4. Show the matrix inserted by the user.
            for (i=0; i<rows; i++){
                for (j=0; j<columns; j++){
                    System.out.print( "[" + matrix[i][j] + "]" + " ");
                    }
                System.out.println();
            }

            checkEquality(matrix, rows, columns, same);




        }
        public static boolean comprobarIgualdad(int matriz[][], int filas, int columnas, boolean igual){
            int i, j;

            for (i=0; i<rows; i++){
                for (j=0; j<columns; j++){
                    if(matrix[i] == matrix[j])
                        same = true;
                    else
                        same = false;
                    }

            }
            return same;





        }

    }

现在,我的问题是:我不知道如何制作垂直和水平遍历矩阵并检查它们是否相同的方法。这些天我一直在想,但我找不到答案,所以我想在这里寻求帮助。我想自己解决这个问题,但我想我做不到,所以任何帮助都会受到好评。如果您对我的代码有任何疑问,请告诉我!

【问题讨论】:

    标签: java arrays matrix


    【解决方案1】:

    嗯,你可能知道,二维数组的运行总是在嵌套循环中完成的。您必须使用每个可能的星座调用checkEquality()(即,第 0 行和第 0 列,第 0 行和第 1 列,第 0 行和第 2 列,第 1 行和第 0 列,...)。

    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[i].length; j++) {
            if (checkEquality(matrix, i, j)) // no need for same here in my opinion
               System.out.println("Row " + i + " and " + " column " + j + " are equal.");
        }
    }
    [...]
    private static boolean checkEquality(int[][] matrix, int row, int column) {
        for (int i = 0; i < matrix.length; i++) { // only works if number of rows equal number of columns
            if (matrix[row][i] != matrix[i][column]) { // running through rows and columns
                // if a single value is not equal you can stop searching and return false
                return false;
           }
        }
        return true; // otherwise it is true
    }
    

    【讨论】:

      【解决方案2】:

      如果行的长度与 cols 相同,您可以试试这个。

      private boolean checkRowsEquality(int[][] matrix, int row, int col) {
          for (int i = 0; i < matrix.length; i++) {
                 if (matrix[row][i] != matrix[i][col])
                     return false;
          }
          return true;
      }
      
      
      private boolean checkEquality(int[][] matrix) {
          for (int i = 0; i < matrix.length; i++) {
              for (int j = 0; j < matrix[i].length; j++) {
                  if (checkEquality(matrix, i, j)) 
                     return true;
              }
          }
          return false;
      }
      

      希望对您有所帮助。我也来自西班牙。苏尔特:D

      【讨论】:

      • 此方法只会检查所有列和行是否相等,但它应该检查每一行/列对。 if(isValid) return true 也是多余的,为什么不简单地使用 break;return isValid;
      • 你确定吗?请检查代码。另外break; 是一种不好的做法
      • 您的代码比较 matrix[0][0] 和 matrix[0][0]、matrix[0][1] 和 matrix[1][0]、matrix[0][2]和矩阵[2][0](例如,行 = 0 和列 = 0)。接下来比较matrix[1][0]和matrix[0][1],matrix[1][1]和matrix[1][1],matrix[1][2]和matrix[2][1]最后比较matrix[2][0]和matrix[0][2],matrix[2][1]和matrix[1][2],matrix[2][2]和matrix[2][ 2]。简而言之,它比较第 0 行和第 0 列、第 1 行和第 1 列以及第 2 行和第 2 列,但不比较第 0 行和第 1、2 列、第 1 行第 0、第 2 列等等。
      • 好的,抱歉。我的错。我想念明白。我现在要改了
      • 如果你的循环和你的一样短,break 也是不错的做法。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 2013-01-29
      • 2022-01-27
      • 1970-01-01
      • 2021-12-30
      • 2012-04-11
      相关资源
      最近更新 更多