In: Computer Science
The two-dimensional arrays list1 and list2 are identical if they have the same contents. Write a method that returns true if they are identical and false if they are not. Use the following header: public static boolean equals(int [][] list1, int [][] list2)
Write a test program that prompts the user to enter two 3 x 3 arrays of integers and displays whether the two are identical.
Enter list1:
Enter list2:
The two arrays are identical or The two arrays are not identical
import java.util.Scanner;
public class IdenticalArrays {
public static boolean equals(int [][] list1, int [][] list2) {
if (list1.length == list2.length) {
for (int i = 0; i < list1.length; i++) {
if (list1[i].length == list2[i].length) {
for (int j = 0; j < list1[i].length; j++) {
if (list1[i][j] != list2[i][j]) {
return false;
}
}
} else {
return false;
}
}
return true;
}
return false;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[][] m1 = new int[3][3], m2 = new int[3][3];
System.out.println("Enter first matrix: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
m1[i][j] = in.nextInt();
}
}
System.out.println("Enter second matrix: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
m2[i][j] = in.nextInt();
}
}
if (equals(m1, m2)) {
System.out.println("Arrays are equal");
} else {
System.out.println("Arrays are not equal");
}
}
}