6.27 JAVA- DO NOT COPY CODE FROM ANOTHER SITE (Identical arrays) The arrays list
ID: 3694160 • Letter: 6
Question
6.27 JAVA- DO NOT COPY CODE FROM ANOTHER SITE
(Identical arrays) The arrays list1 and list2 are identical if they have the same contents. Write a method that returns true if list1 and list2 are identical, using the following header: public static boolean equals(int[] list1, int[] list2) Write a test program that prompts the user to enter two lists of integers and displays 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
Explanation / Answer
import java.util.*;
class arr_equal
{
public static void main(String args[])
{
int list1[] = new int[100];
int list2[] = new int[100];
int i,j,n1,n2;
Scanner o = new Scanner(System.in);
System.out.print("Enter number of elements in list1:");
n1=o.nextInt();
System.out.print("Enter elements in list1:");
for(i=0;i<n1;i++)
{
list1[i]=o.nextInt();
}
System.out.print("Enter number of elements in list2:");
n2=o.nextInt();
System.out.print("Enter elements in list2:");
for(i=0;i<n2;i++)
{
list2[i]=o.nextInt();
}
if(equals(list1, list2))
{
System.out.println("The two arrays are identical");
}
else
{
System.out.println("The two arrays are not identical");
}
}
public static boolean equals(int[] list1, int[] list2)
{
int i,j;
for(i=0;i<3;i++)
{
if(list1[i]!=list2[i])
{
return (false);
}
}
return (true);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.