2个矩阵之间的行相等

时间:2018-07-18 17:07:03

标签: java arrays matrix equals

我需要检查一个矩阵的行是否等于第二个矩阵的行。我尝试了几种不同的方法,但似乎找不到一种可行的简单方法。

这是我用于测试的文件:

    public static void main(String[] args) {
    int[][] a0 = null;
    int[][] b0 = null;
    int[][] a1 = {};
    int[][] b1 = {};
    System.out.println(ex1(a0,b0)==false);
    System.out.println(ex1(a0,b1)==false);
    System.out.println(ex1(a1,b0)==false);
    System.out.println(ex1(a1,b1)==false);
    int[][] a2 = {{2, 1  , 5}
                 ,{3, 10 , 6}
                 ,{4, 100, 7}};
    int[][] b2 = {{4, 7 , 1  }
                 ,{3, 5 , 10 }
                 ,{2, 6 , 100}};
    System.out.println(ex1(a2,b2)==true );
    int[][] a3 = {{2, 1  , 5}
                 ,{3, 10 , 6}
                 ,{4, 100, 7}};
    int[][] b3 = {{4, 1  , 7}
                 ,{3, 10 , 6}
                 ,{2, 0  , 5}};
    System.out.println(ex1(a3,b3)==false);
    int[][] a4 = {{2, 1  , 5}
                 ,{3, 10 , 6}
                 ,{4, 100, 7}};
    int[][] b4 = {{1 , 4 , 7}
                 ,{10, 3 , 6}
                 ,{0 , 2 , 5}};
    System.out.println(ex1(a4,b4)==false);
    int[][] a5 = {{1  , 2, 5  }
                 ,{10 , 3, 6  }
                 ,{100, 4, 7  }};
    int[][] b5 = {{1  , 4, 1  }
                 ,{10 , 3, 10 }
                 ,{0  , 2, 100}};
    System.out.println(ex1(a5,b5)==true );
    int[][] a6 = {{1  , 2  , 5}
                 ,{10 , 3  , 6}
                 ,{100, 4  , 7}};
    int[][] b6 = {{1  , 1  , 7}
                 ,{1  , 10 , 6}
                 ,{0  , 100, 5}};       
    System.out.println(ex1(a6,b6)==true );
}

}

这是我的代码:

public static boolean ex1(int[][] a, int[][] b){
     if(a == null || a.length < 1 || b == null || b.length < 1){
         return false;
     }
     for(int i = 0; i < a.length; i++){
         int cont = 0;
         for(int j = 0; j < a.length; j++){
             for(int z = 0; z < a.length; z++){
                 if(a[j][i] == b[z][i]){
                     cont++;
                 }
                 if(cont == a[i].length){
                     return true;
                 }
             }
         }
     }
     return false;
 }

这是测试文件的输出,所有内容都应为true:Output Image

有人可以帮我吗?

2 个答案:

答案 0 :(得分:3)

您可以简单地使用Arrays.equals(arr1,arr1)函数。尽管该示例适用于一维数组,但应适用于二维数组:

import java.util.Arrays;

public class Main {
   public static void main(String[] args) throws Exception {
      int[] ary = {1,2,3,4,5,6};
      int[] ary1 = {1,2,3,4,5,6};
      int[] ary2 = {1,2,3,4};
      System.out.println("Is array 1 equal to array 2?? " +Arrays.equals(ary, ary1));
      System.out.println("Is array 1 equal to array 3?? " +Arrays.equals(ary, ary2));
   }
}

输出:

Is array 1 equal to array 2?? true
Is array 1 equal to array 3?? false

答案 1 :(得分:1)

我会更改您的ex1(),以便您检查不匹配的条件并返回false。如果您到达方法的底部,则矩阵相等。

类似的东西:

public class StackOverflow {
    public static void main(String[] args) {
        int[][] a0 = null;
        int[][] b0 = null;
        int[][] a1 = {};
        int[][] b1 = {};
        System.out.println(ex1(a0, b0) == false);
        System.out.println(ex1(a0, b1) == false);
        System.out.println(ex1(a1, b0) == false);
        System.out.println(ex1(a1, b1) == false);
        int[][] a2 = { { 2, 1, 5 }, { 3, 10, 6 }, { 4, 100, 7 } };
        int[][] b2 = { { 4, 7, 1 }, { 3, 5, 10 }, { 2, 6, 100 } };
        System.out.println(ex1(a2, b2) == true);
        int[][] a3 = { { 2, 1, 5 }, { 3, 10, 6 }, { 4, 100, 7 } };
        int[][] b3 = { { 4, 1, 7 }, { 3, 10, 6 }, { 2, 0, 5 } };
        System.out.println(ex1(a3, b3) == false);
        int[][] a4 = { { 2, 1, 5 }, { 3, 10, 6 }, { 4, 100, 7 } };
        int[][] b4 = { { 1, 4, 7 }, { 10, 3, 6 }, { 0, 2, 5 } };
        System.out.println(ex1(a4, b4) == false);
        int[][] a5 = { { 1, 2, 5 }, { 10, 3, 6 }, { 100, 4, 7 } };
        int[][] b5 = { { 1, 4, 1 }, { 10, 3, 10 }, { 0, 2, 100 } };
        System.out.println(ex1(a5, b5) == true);
        int[][] a6 = { { 1, 2, 5 }, { 10, 3, 6 }, { 100, 4, 7 } };
        int[][] b6 = { { 1, 1, 7 }, { 1, 10, 6 }, { 0, 100, 5 } };
        System.out.println(ex1(a6, b6) == true);
    }

    public static boolean ex1(int[][] a, int[][] b) {
        if (a == null || a.length < 1 || b == null || b.length < 1) {
            return false;
        }

        // Check that both arrays have the same number of rows
        if (a.length != b.length) {
            return false;
        }

        for (int i = 0; i < a.length; i++) {
            // Check that each row has the same number of columns
            if (a[i].length != b[i].length) {
                return false;
            }

            // Check the contents of each row
            for (int j = 0; j < a[i].length; j++) {
                if (a[i][j] != b[i][j]) {
                    return false;
                }
            }
        }

        return true;
    }
}

结果:

true
true
true
true
false // These don't match, but you're checking that they do match
true  // These don't match, and you're checking that they don't match
true  // These don't match, and you're checking that they don't match 
false // These don't match, but you're checking that they do match
false // These don't match, but you're checking that they do match

根据您在问题中显示的数据,所有这些数据都将不等于true。