Question: With Strings, we need to use the equals method to determine if two Strings are the same. Lets write an equals method for the ArrayList

With Strings, we need to use the equals method to determine if two Strings are the same.

Lets write an equals method for the ArrayList class that returns true if two ArrayLists are the same, and false if the ArrayLists are not identical. An ArrayList is identical if all of the values at each index are the same.

import java.util.ArrayList; public class isEqual { public static void main(String[] args) { //This code is just to test your equals method ArrayList list1 = new ArrayList(); list1.add(10); list1.add(9); list1.add(5); list1.add(2); list1.add(9); ArrayList list2 = new ArrayList(); list2.add(10); list2.add(9); list2.add(5); list2.add(2); list2.add(9); boolean isEqual = equals(list1, list2); System.out.println("List 1 is equal to List 2: "+isEqual); ArrayList list3 = new ArrayList(); list3.add(1); list3.add(9); list3.add(5); list3.add(2); list3.add(9); boolean isEqual2 = equals(list2, list3); System.out.println("List 2 is equal to List 3: "+isEqual2); } //Write your method here! public static boolean equals(ArrayList array1, ArrayList array2) { } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!