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

Arraylist Union and Intersection using iterator my union and intersection is wei

ID: 3801971 • Letter: A

Question

Arraylist Union and Intersection using iterator

my union and intersection is weird... it is sorted arraylist but what i expected was for union:{1 2 3 4 5 6} and inetersection:{4 5} but it seems union just prints out everything iguess and the intersection printsout nothing

below is teh code

public static void getUnion(){
   System.out.print("A Union B is:{");
   Iterator<Integer> itr = m1.iterator();
   while(itr.hasNext())
   {
   System.out.print(itr.next());
   if(itr.hasNext())
   {
   System.out.print(" ");
   }
   }
   itr = m2.iterator();
   while(itr.hasNext())
   {
   Integer temp = itr.next();
   if(!m1.contains(temp))
   {
   System.out.print(temp);
   }
     
   if(itr.hasNext())
   {
   System.out.print(" ");
   }
   }
     
     
   System.out.println("}");
   }

      
       public static void getIntersection(){
           ArrayList<Integer> in= new ArrayList<Integer>();
           Iterator<Integer> itr =m1.iterator();
           Iterator<Integer> itr2 = m2.iterator();
           System.out.print("A Intersect B is: {");
       itr = m2.iterator();
       while(itr.hasNext())
       {
       Integer temp = itr.next();
       if(m1.contains(temp))
       {
       System.out.print(temp);
       if(itr.hasNext())
       {
       System.out.print(", ");
       }
       }
       }
       System.out.println("}");
       }

Console union ava cation] C:Pro am Files Java 1.8.0 Abiniavaw.exe (Mar 21, 2017, 10:28:07 PM 1. Add element in A 2.Add element in B 3.Get union 4.Get Intersection S.Display current A and B 6. exit A Elements are: 1 2 3 4 5 B Elements are: 4 5 6 Please select the menu 1.Add element in A 2.Add element in B 3. Get union 4.Get Intersection 5.Display current A and B 6 exit A Union B is {1 2 3 4 54 5 61 Please select the menu: 1.Add element in A 2.Add element in B 3 Get union 4 Get Intersection s Display current A and B 6. exit A Intersect B is Please select the menu 1.Add element in A 2.Add element in B Get union 4. Get Intersection 5. Display current A and B 6 exit

Explanation / Answer

import java.util.*;

class SetOperations
{
   public static void main (String[] args)
   {
        Scanner scan = new Scanner(System.in);
        int element;
      
        ArrayList<Integer>m1 = new ArrayList<Integer>();
        ArrayList<Integer>m2 = new ArrayList<Integer>();
      
        System.out.println(" Enter the number of elements in m1 : ");
        int size1 = scan.nextInt();
      
        System.out.println(" Enter the elements of m1 : ");
        for(int i=0;i<size1;i++)
        {
            element = scan.nextInt();
            m1.add(element);
          
        }
      
        System.out.println(" Enter the number of elements in m2 : ");
        int size2 = scan.nextInt();
      
        System.out.println(" Enter the elements of m2 : ");
        for(int i=0;i<size2;i++)
        {
            element = scan.nextInt();
            m2.add(element);
          
        }
      
        System.out.println(" Union : ");
        getUnion(m1,m2);
      
        System.out.println(" Intersection : ");
        getIntersection(m1,m2);
      
      
   }
   public static void getUnion(ArrayList<Integer>m1,ArrayList<Integer>m2)
   {
           System.out.print("A Union B is:{");
           Iterator<Integer> itr = m1.iterator();
           while(itr.hasNext())
           {
               System.out.print(itr.next());
               if(itr.hasNext())
               {
                   System.out.print(" ");
               }
           }
           System.out.print(" ");
           for (Integer t : m2) {
            if(!m1.contains(t)) {     //print the elements which do not match in m2
              System.out.print(t+ " ");
            }
        }
         
         
           System.out.println("}");
      }

      
         public static void getIntersection(ArrayList<Integer>m1,ArrayList<Integer>m2)
         {
             ArrayList<Integer> in= new ArrayList<Integer>();
             Iterator<Integer> itr =m1.iterator();
             Iterator<Integer> itr2 = m2.iterator();
             System.out.print("A Intersect B is: {");
               itr = m2.iterator();
               while(itr.hasNext())
               {
                  Integer temp = itr.next();
                   if(m1.contains(temp))
                   {
                       System.out.print(temp);
                       if(itr.hasNext())
                       {
                           System.out.print(" ");
                       }
                   }
               }
               System.out.println("}");
         }
}


Output:

Enter the number of elements in m1 : 3
Enter the elements of m1 :

45

67

34

Enter the number of elements in m2 : 4
Enter the elements of m2 :

45

78

66

23

Union :

A Union B is : {45 67 34 78 66 23 }

Intersection :

A Intersection B is : {45 }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote