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

Arraylist Union and Intersection using iterator

Arraylist Union and Intersection using iterator my union and intersection is weird...

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 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 in= new ArrayList(); Iterator itr =m1.iterator(); Iterator 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 Abin\iavaw.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

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!