如果两个二维数组m1和m2相应的元素相等的话,则认为它们是严格地相同的。编写一个方法,如果m1和m2是严格相同的话,返回true。使用下面的方法头:
public static boolean equals(int[][] m1, int[][] m2)
编写一个测试程序,提示用户输入2个3*3的整数数组,显示两个矩阵是否是一个相同的。
package pack2;
import java.util.Arrays;
import java.util.Scanner;
public class StrictSameArrays {
public static void main(String[] args) {
strictSameArrays();
}
// 51 22 25 6 1 4 24 54 6
// 51 25 22 6 1 4 24 54 6
public static void strictSameArrays() {
try(Scanner input = new Scanner(System.in);) {
System.out.print("Enter the size of two arrays for the length of " +
"row and column: ");
int size = input.nextInt();
int[][] list1 = new int[size][size];
int[][] list2 = new int[size][size];
System.out.print("Enter list1: ");
inputData(input, list1);
System.out.print("Enter list2: ");
inputData(input, list2);
System.out.println("The two arrays are " + (equals(list1, list2) ? "" :
"not ") + "strictly identical");
}
}
/**接收数据输入*/
public static void inputData(Scanner input, int[][] m) {
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[i].length; j++)
m[i][j] = input.nextInt();
}
/**两个数组是否严格相同?*/
public static boolean equals(int[][] m1, int[][] m2) {
for (int i = 0; i < m2.length; i++)
if(!(Arrays.equals(m1[i], m2[i])))
return false;
return true;
}
}