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

The interface of Iterator<E> contains next(), hasNext(), and remoce(). Assume th

ID: 3851658 • Letter: T

Question

The interface of Iterator<E> contains next(), hasNext(), and remoce().

Assume the following: The Iterator interface is as discussed in lecture. The ArrayList class implements the iterable and ListADT interfaces. The items in a list can be compared with the equals method or the compareTomethod. Complete the Java method specified below, making use of iterators must explicitly use iterators for traversing lists must not use the contains method must not modify the contents of the parameter public static ArrayList flatten (Array List list) { //If list is null, throw a NullPointerException //If list is empty, return a new empty list. //Otherwise create and return a new list that contains the objects contained in //any of the lists contained in list, removing duplicate items. The order of //items in the returned list should be the same as the order in which they //first appear in the original list. //Example: list: [[1, 2, 3], [4, 3, 6], [1, 5]] result: [1, 2, 3, 4, 6, 5]

Explanation / Answer

1. Source Code without using remove() method

package com.sample;

import java.util.ArrayList;
import java.util.Iterator;

public class Test {

   public static void main(String[] args) {

       ArrayList<Integer> list1 = new ArrayList<Integer>();
       list1.add(1);
       list1.add(2);
       list1.add(3);

       ArrayList<Integer> list2 = new ArrayList<Integer>();
       list2.add(4);
       list2.add(3);
       list2.add(6);

       ArrayList<Integer> list3 = new ArrayList<Integer>();
       list3.add(1);
       list3.add(5);

       ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
       list.add(list1);
       list.add(list2);
       list.add(list3);

       //Calling flatten with list of list
       flatten(list);

   }

   public static ArrayList<Integer> flatten(ArrayList<ArrayList<Integer>> list) {
       ArrayList<Integer> flatList = new ArrayList<Integer>();
      
       //Checking if Main List is Null or not, if Null throwing Null Pointer exception
       if (list != null) {
          
           //Iterating Main List
           Iterator<ArrayList<Integer>> itAllList = list.iterator();
           while (itAllList.hasNext()) {
              
               //Getting each Sublist
               ArrayList<Integer> internalList = itAllList.next();
              
               //Checking if Each Sublist is Null or not, if Null throwing Null Pointer exception
               if (internalList != null) {
                  
                   //Iterating Each Sublist
                   Iterator<Integer> itList = internalList.iterator();
                   while (itList.hasNext()) {
                      
                       //Getting each value of Sublist
                       int value = itList.next();
                      
                       //Checking if value is already present in the returned list or not, if not then only adding value
                       if (!flatList.contains(value)) {
                           //Adding value in returned list
                           flatList.add(value);
                       }
                   }
               } else {
                   throw new NullPointerException("Sub List is null");
               }
           }
       } else {
           throw new NullPointerException("Main List is null");
       }
       System.out.println(flatList);
       return flatList;
   }
}

Output:

[1, 2, 3, 4, 6, 5]

2. Source Code using remove() method

package com.sample;

import java.util.ArrayList;
import java.util.Iterator;

public class Test {

   public static void main(String[] args) {

       ArrayList<Integer> list1 = new ArrayList<Integer>();
       list1.add(1);
       list1.add(2);
       list1.add(3);
       list1.add(6);

       ArrayList<Integer> list2 = new ArrayList<Integer>();
       list2.add(4);
       list2.add(3);
       list2.add(6);
       list2.add(8);

       ArrayList<Integer> list3 = new ArrayList<Integer>();
       list3.add(1);
       list3.add(5);
       list3.add(7);

       ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
       list.add(list1);
       list.add(list2);
       list.add(list3);

       //Calling flatten with list of list
       flatten(list);

   }

   public static ArrayList<Integer> flatten(ArrayList<ArrayList<Integer>> list) {
       ArrayList<Integer> flatList = new ArrayList<Integer>();
      
       //Checking if Main List is Null or not, if Null throwing Null Pointer exception
       if (list != null) {
          
           //Iterating Main List
           Iterator<ArrayList<Integer>> itAllList = list.iterator();
           while (itAllList.hasNext()) {
              
               //Getting each Sublist
               ArrayList<Integer> internalList = itAllList.next();
              
               //Checking if Each Sublist is Null or not, if Null throwing Null Pointer exception
               if (internalList != null) {
                  
                   //Iterating Each Sublist
                   Iterator<Integer> itList = internalList.iterator();
                   while (itList.hasNext()) {
                      
                       //Getting each value of Sublist
                       int value = itList.next();
                      
                       //Adding value in returned list
                       flatList.add(value);
                   }
               } else {
                   throw new NullPointerException("Sub List is null");
               }
              
              
               //Removing duplicate Items
               for (int i=0;i<flatList.size();i++) {
                   for (int j=i+1 ;j<flatList.size();j++) {
                       if (flatList.get(i) == flatList.get(j)){
                           //removing item from specific index
                           flatList.remove(j);
                       }
                   }
               }
              
           }
       } else {
           throw new NullPointerException("Main List is null");
       }
       System.out.println(flatList);
       return flatList;
   }
}

Output:

[1, 2, 3, 6, 4, 8, 5, 7]

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