Java 两个矩阵之间行的相等性
我需要检查一个矩阵的行是否等于第二个矩阵的行。我尝试过几种不同的方法,但似乎找不到一种简单有效的方法 以下是我用于测试的文件:Java 两个矩阵之间行的相等性,java,arrays,matrix,equals,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
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;
}
publicstaticbooleaneex1(int[]a,int[]b){
如果(a==null | | a.length<1 | | b==null | | b.length<1){
返回false;
}
for(int i=0;i
以下是测试文件的输出,所有内容都应为真:
有人能帮我吗?您可以简单地使用函数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
您可以简单地使用函数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
我将更改您的
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;
}
}
对于您在问题中显示的数据,它们的计算结果都不是真的。我将更改您的
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;
}
}
根据您在问题中显示的数据,它们的计算结果都不会为真。我认为我不能使用“arrays.equals”^^,因为这是一个循环练习(for/while)我认为我不能使用“arrays.equals”^,因为这是一个循环练习(for/while)非常感谢!我已经写了代码,但是测试文件不是我写的,我只是复制了^^^@AlfredoPipoli,不客气。请检查这个答案,如果它解决了你的问题。非常感谢!我已经写了代码,但是测试文件不是我写的,我只是复制了^^^@AlfredoPipoli,不客气。请检查这个答案,如果它解决了你的问题。