Java please help asap 7.27 (Identical arrays) The arrayslistl and ist2 are ident
ID: 644926 • Letter: J
Question
Java please help asap 7.27 (Identical arrays) The arrayslistl and ist2 are identical if they have the same contents. Write a method that returns true iflistl andlist2 are identi- call, using the following header: public static boolean equals CintCl list1, int list2) Programming Exercises 283 Write a test program that prompts the user to enter two lists of integers and dis- plays whether the two are identical. Here are the sample runs. Note that the first number in the input indicates the number of the elements in the list. This number is not part of the list. Enter list1: S 2 5 6 6 1 Enter list2: 5 5 2 6 16 Two lists are identical Enter list1: 5 5 5 6 6 1 Enter list2: S 2 5 6 1 6 Two lists are not identicalExplanation / Answer
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter list1: ");
int size1 = input.nextInt();
int[] list1 = new int[size1];
for (int i = 0; i < list1.length; i++)
list1[i] = input.nextInt();
System.out.print("Enter list2: ");
int size2 = input.nextInt();
int[] list2 = new int[size2];
for (int i = 0; i < list2.length; i++)
list2[i] = input.nextInt();
if (equal(list1, list2)) {
System.out.println("Two lists are identical");
}
else
{
System.out.println("Two lists are not identical");
}
}
public static boolean equal(int[] list1, int[] list2) {
if(list1.length == list2.length) {
Arrays.sort(list1);
Arrays.sort(list2);
}
else
return false;
for (int i = 0; i < list1.length; i++) {
if (list1[i] != list2[i])
return false;
}
return true;
}
}
Output1:
Enter list1: 5 5 5 5 5 4
Enter list2: 5 5 5 5 5 4
Two lists are identical
output 2:
Enter list1: 5 4 3 2 5 6
Enter list2: 5 4 5 3 4 5
Two lists are not identical
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.