题解 | #矩阵相等判定#
矩阵相等判定
http://www.nowcoder.com/practice/2f058b1e89ae43f7ab650c0fd0b71fa8
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int[][] arr1 = new int[n][m];
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1[i].length; j++) {
arr1[i][j] = scan.nextInt();
}
}
int[][] arr2 = new int[n][m];
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr2[i].length; j++) {
arr2[i][j] = scan.nextInt();
}
}
int x = 0;
for(int i = 0; i < n; i++) {
if(Arrays.equals(arr1[i], arr2[i]) == false)
x++;
}
if (x == 0) {
System.out.println("Yes");
} else {
System.out.println("No");
}
//下面代码同样可以通过
// if (arrayEquals(arr1, arr2)) {
// System.out.println("Yes");
// } else {
// System.out.println("No");
// }
}
//arrayEquals方法
public static boolean arrayEquals(int[][] arr1, int[][] arr2) {
if (arr1 == arr2)
return true;
if (arr1 == null || arr2 == null)
return false;
if (arr1.length != arr2.length)
return false;
if (arr1[0].length != arr2[0].length)
return false;
for (int i = 0; i < arr1.length; i++) {
if (Arrays.equals(arr1[i], arr2[i]) == false)
return false;
}
return true;
}
}
Java基础练习题 文章被收录于专栏
都是一些基础的语法题目,每天可以刷几道。