Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.ArrayList; import java.util.List; public class MyArrayList { pu

ID: 3802761 • Letter: I

Question

 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() + ", ");                 }         } } 

Explanation / Answer

This is Main as well as Diver class:

package com.test;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.TreeSet;

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);

System.out.println(" The integers after removing duplicates are : ");
removeDuplicates(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);   

System.out.println(" The integers after union are : ");
//print(list);
union(list, list2);

}

public static int findMax(ArrayList list)
{   
int max = ((Integer) list.get(0)).intValue();

for (int i = 0; i < list.size(); i++){
Integer n = (Integer) 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)
{   
//Best way to used LinkedHashSet
//To be completed
   LinkedHashSet<Integer> list1=new LinkedHashSet<Integer>(list);
   ArrayList list2=new ArrayList<>(list1);
  
   print(list2);
     
}

//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
   ArrayList<ArrayList> list = new ArrayList<ArrayList>();
   list.add(list1);
   list.add(list2);
  
   printUnion(list);
}   

//This method prints items stored in an arrayList object.
public static void print(ArrayList list)
{
for (int i = 0; i < list.size(); i++){
System.out.print(((Integer) list.get(i)).intValue() + ", ");
}

}
public static void printUnion(ArrayList list)
{
   for (int i = 0; i < list.size(); i++){
       //System.out.print(list.get(i));
       for (int j = 0; j <((ArrayList) list.get(i)).size(); j++){
           System.out.print(((ArrayList) list.get(i)).get(j) + ", ");
   }
      
}
     
}
  
}

Output:

The max integer in the list is : 25
The integers with duplicates are :
23, 5, 3, 7, 5, 6, 4, 25, 2, 2, 5, 2, 4,
The integers after removing duplicates are :
23, 5, 3, 7, 6, 4, 25, 2,

The integers before union are :
23, 5, 3, 7, 5, 6, 4, 25, 2, 2, 5, 2, 4,

8, 5, 9, 7, 6,
The integers after union are :
23, 5, 3, 7, 5, 6, 4, 25, 2, 2, 5, 2, 4, 8, 5, 9, 7, 6,