Question: Include an add(E) method in your ListIterator interface. It should add a value to the List just prior to the implicit cursor. (See the API
Include an add(E) method in your ListIterator interface. It should add a value to the List just prior to the implicit cursor. (See the API in the java.util package)
/** Insert the given value just prior to the implicit cursor position. A subsequent call to previous()
should return the inserted value, and a subsequent call to next() should be unaffected.
*/
public void add (E value);
Implement the add(E) method in your ArrayListIterator and RefListIterator classes. Test your solution using DriverLabListIterator.
======================================
ArrayListIterator.java
package list;
public class ArrayListIterator extends ArrayIterator implements ListIterator {
private boolean forward = true; ArrayListIterator(List list) { super(list); } ArrayListIterator(List list, int start) { super(list); ndx = start -1; } public boolean hasPrevious() { return ndx>=0; } public E previous() { forward = false; ndx--; return list.get(ndx+1); } public void remove() { if(forward) { list.remove(ndx); ndx--; } else { list.remove(ndx+1); } } public E next() { forward = true; return super.next(); } }
===============================
RefListIterator.java
package list;
public class RefListIterator extends RefIterator implements ListIterator {
private boolean forward = true; RefListIterator(LinkedList list) { super(list); } RefListIterator (LinkedList list, int start) { super(list); for(int i=0; i ===================================================
Driver
package listDriver;
import java.util.ListIterator;
import list.*; /** * Test Iterators and ListIterators. * * @author (sdb) * @version (2020) */ public class DriverLabListIterator { public static void main(String [] args) { List names; System.out.println ("Testing Iterators for ArrayLists"); testIterators (new ArrayList()); System.out.println ("Testing Iterators for LinkedLists"); testIterators (new LinkedList()); System.out.println();
System.out.println ("Testing ListIterators for ArrayLists"); testListIterators (new ArrayList ()); System.out.println (" Testing ListIterators for LinkedLists"); testListIterators (new LinkedList ()); }
private static void testIterators (List names) { names.add ("jim"); names.add ("mary"); names.add ("joe"); names.add ("sue"); Iterator itty = names.iterator(); while (itty.hasNext()) if (itty.next().length() > 3) itty.remove(); System.out.println (names); // should be [jim, joe, sue] } private static void testListIterators (List names) { names.add ("jim"); names.add ("mary"); names.add ("joe"); names.add ("sue"); System.out.println (names); ListIterator itty = names.listIterator(); System.out.println ("Test the add method:"); System.out.println (itty.next()); // "jim" System.out.println (itty.next()); // "mary" itty.add ("harry"); System.out.println (names); // [jim, mary, harry, joe, sue] System.out.println (itty.next()); // "joe" itty.add ("bill"); System.out.println (itty.previous()); // bill itty.add ("james"); System.out.println (itty.next()); // bill System.out.println (names); // [jim, mary, harry, joe, james, bill, sue] if (names.size() != 7) System.err.println ("Error in ListIterator"); }
}
============
ListIterator.java
package list;
public interface ListIterator extends Iterator {
public E previous(); public void add(E value); }
===============================
Iterator.java
package list;
public interface Iterator { /** @return true iff there are more values to be obtained * with this Iterator. */ boolean hasNext(); /** @return the next value in the associated collection. * Pre: hasNext() is true. */ E next(); /** * Remove the last value obtained with this Iterator. * Pre: next() has been called at least once since the last * call to remove(). */ void remove();
}