Two arrays are identical arrays if they are the same size and have the same elem
ID: 646492 • Letter: T
Question
Two arrays are identical arrays if they are the same size and have the same elements, although the elements do not have to be in the same positions. Write a program called IdenticalArrays that asks the user to enter in two 3x3 arrays ofintegers, and then your program will display whether the two arrays are identical.
To accomplish this, your main() method will get all of the values (as integers) and place them into the two arrays, and then pass the arrays to a method you must write called equals(), which will return true if the elements are identical, and false if not. Your equals() method will have the following signature:
Design the main() method of your program such that it allows the user to re-run the program with different inputs (i.e., use a loop structure).
Here is a sample output:
Explanation / Answer
Java Program:
import java.util.Scanner;
import java.util.Arrays;
public class StrictlyIdenticalArr{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//create matrix by Arrays from user input
double[][] m1 = new double[3][3];
System.out.println("Enter list1: ");
for (int row = 0; row < m1.length; row++){
for (int column = 0; column < m1[row].length; column++){
m1[row][column] = input.nextDouble();
}//end for
}//end for
//create matrix by Arrays from user input
double[][] m2 = new double[3][3];
System.out.println("Enter list2: ");
for (int row = 0; row < m2.length; row++){
for (int column = 0; column < m2[row].length; column++){
m2[row][column] = input.nextDouble();
}//end for
}//end for
if (m1.equals(m2)){
System.out.println("The two lists are strictly identical");
}//end if
else{
System.out.println("The two lists are not strictly identical");
}//end else
}//end main
public static boolean equals(int[][] m1, int[][] m2){
if (m1.length != m2.length){
return false;
}//end if
Arrays.sort(m1);
Arrays.sort(m2);
for (int i = 0; i < m1.length; i++){
if (m1[i] != m2[i])
return false;
}//end if
return true;
}//end equals
}//end StrictlyIdenticalArr
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.