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

For the following method of interface ListIterator, answer the questions: void s

ID: 3862207 • Letter: F

Question

For the following method of interface ListIterator, answer the questions:

void set(E obj);

In what class(es) could this method be implemented?

In what class should this method be implemented?

Write a thorough test case for the method:

Write the method:

What is the efficiency of this method? Justify your answer.

For the following method of interface ListIterator, answer the questions:

void set(E obj);

In what class(es) could this method be implemented?

In what class should this method be implemented?

Write a thorough test case for the method:

Write the method:

What is the efficiency of this method? Justify your answer.

Explanation / Answer

1. Any class which implements the ListIterator Interface can implement set method. set method replace the element last returned by a call to next or previous with a given object.

2. set method or the ListIterator, in general, should be implemented for classes which has some sequential data structure attached to it like ArrayList, Set, Vector etc. A ListIterator basically iterate over this data structure and perform the action accordingly.

3. Test:

import java.util.ArrayList;
import java.util.ListIterator;

public class TestListIterator {
   public static void main(String args[]) {
       ArrayList<String> al = new ArrayList<String>();
       al.add("Apple");
       al.add("Mango");
       al.add("Banana");

       ListIterator<String> itr = al.listIterator();
       System.out.println("traversing elements ....");
       while (itr.hasNext()) {
           String val = itr.next();
           System.out.println(val);
           if(val.equals("Mango")){
               itr.set("Orange");
           }
       }
       System.out.println(al);
   }
}

4. The fundamental requirement of an Iterator is its .next() method. Each .next() takes O(1) (in case of a LinkedList) which combines to O(n). If we put this inside a loop to use a set() method, the total time complexiety comes out to be O(n^2). As compared to normal loop, it seems to be less efficient but mostly the efficiency depends on the type of data structure on which the Iterator is working.

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