/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/*(Identical arrays) The two-dimensional arrays m1 and m2 are identical if they have the same contents.
Write a method that returns true if m1 and m2 are identical, using the following header:
public static boolean equals(int[][] m1, int[][] m2)
Write a test program that prompts the user to enter two 3 X 3 arrays of integers
and displays whether the two are identical. Here are the sample runs.
Enter list1: 51 25 22 6 1 4 24 54 6
Enter list2: 51 22 25 6 1 4 24 54 6
The two arrays are identical
Enter list1: 51 5 22 6 1 4 24 54 6
Enter list2: 51 22 25 6 1 4 24 54 6
The two arrays are not identical*/
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
Scanner input
= new Scanner
(System.
in);
//Create 2 - 3 x 3 Matrices - list1 & list2
int list1[][] = new int[3][3];
int list2[][] = new int[3][3];
//Allow user to enter values for list1
System.
out.
print("Enter List 1: ");
for(int x = 0; x < list1.length; x++)
{
for(int y = 0; y < list1[0].length; y++)
{
list1[x][y] = input.nextInt();
}
}
//Allow user to enter values for list2
System.
out.
print("Enter List 2: ");
for(int y = 0; y < list2.length; y++)
{
for(int z = 0; z < list2[0].length; z++)
{
list2[y][z] = input.nextInt();
}
}
//Close the scanner object
input.close();
//Use the create equals method to tell user
//if the lists are identical or not
if(equals(list1, list2))
{
System.
out.
println("The two arrays are identical"); }
else
{
System.
out.
println("The two arrays are not identical"); }
}
//This method is going check if two arrays are equal
//Method header provided by professor
public static boolean equals(int[][] m1, int[][] m2){
//Check if the user entered in a 3 x 3 Matrix
//Matrix should only have 9 elements
if(m1.length * m1[0].length != 9 && m2.length * m2[0].length != 9)
{
return false;
}
//Translate the M1 Matrix into a single array NewM1
//This will allow me to use the sort method
int NewM1[] = new int[3*3]; //new array will have 9 elements
int x = 0;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
NewM1[x] = m1[i][j];//Consolidate the matrix's elements into the single array
x++;
}
}
//Sort the new single array NewM1 so that I can compare the elements with NewM2
//Translate the matrix into a new single array NewM2
//This will allow me to use the sort method
int NewM2[] = new int[3*3];//New Array will have 9 elements
int c = 0;
for(int a = 0; a < 3; a++)
{
for(int b = 0; b < 3; b++)
{
NewM2[c] = m2[a][b];//Consolidate the matrix's elements into the single array
c++;
}
}
//Sort the new single array NewM2 so that we can compare the elements with NewM1
//****************************
//Use Arrays.equals method to see if the elements
//from the sorted single arrays NewM1 & NewM2 are strictly equal
if(java.
util.
Arrays.
equals(NewM1, NewM2
)) {
return true;
}
//If they are not strictly equal after sorting the elements in order
//The return false because they do not contain the same values
else
{
return false;
}
}
}