Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.util.ArrayList; import java.util.List; public class MyArrayList { public static void main(String[] args) { int [] a = {23, 5, 3, 7, 5, 6, 4,
import java.util.ArrayList; import java.util.List; public class MyArrayList { public static void main(String[] args) { int [] a = {23, 5, 3, 7, 5, 6, 4, 25, 2, 2, 5, 2, 4}; ArrayList list = new ArrayList<>(); for (int i = 0; i < a.length; i++) { Integer n = new Integer(a[i]); list.add(n); } System.out.println("The max integer in the list is : " + findMax(list)); System.out.println("The integers with duplicates are : "); print(list); removeDuplicates(list); System.out.println(" The integers after removing duplicates are : "); print(list); int [] b = {8, 5, 9, 7, 6}; ArrayList list2 = new ArrayList<>(); for (int i = 0; i < b.length; i++) { Integer n = new Integer(b[i]); list2.add(n); } System.out.println(" The integers before union are : "); print(list); System.out.println(" "); print(list2); union(list, list2); System.out.println(" The integers after union are : "); print(list); } public static int findMax(ArrayList list) { int max = list.get(0).intValue(); for (int i = 0; i < list.size(); i++){ Integer n = list.get(i); if ( n.intValue() > max ) max = n.intValue(); } return max; } //This method takes an array list object as input and remove the duplicates in it //The items in the array are objects of Integer type (Java APIs). Use the methods defined for class Integer public static void removeDuplicates(ArrayList list) { //Using brute force method would be fine //To be completed } //This method takes two array lists as input. The resultant list after union is store into the first list. public static void union(ArrayList list1, ArrayList list2) { //Hint: Use "contains() to check if an Integer object is in an arrayList object" //To be completed } //This method prints items stored in an arrayList object. public static void print(ArrayList lst) { for (int i = 0; i < lst.size(); i++){ System.out.print(lst.get(i).intValue() + ", "); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started