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. *List Interface*
package list;
import java.util.ListIterator;
public interface List { E get (int ndx); E set (int ndx, E value); void add (E value); void add(int ndx, E value); E remove (String string); int size(); void clear(); boolean isEmpty(); int indexOf(Object obj); boolean contains (Object obj); public String toString(); boolean remove (Object obj); Iterator iterator(); boolean equals (Object obj); } *Iterator Interface*
package list;
public interface Iterator { boolean hasNext(); E next(); void remove(); } *ArrayListIterator*
package list;
public class ArrayIterator implements Iterator { List list; int ndx = -1; ArrayIterator(List list) { this.list = list; } public boolean hasNext() { return ndx < list.size() -1; } public E next() { ndx++; return list.get(ndx); } public void remove() { list.remove(ndx); ndx--; } } *RefListIterator*
package list;
public class RefIterator implements Iterator { LinkedList list; Node cursor; RefIterator(LinkedList list) { this.list = list; cursor = list.head; } public boolean hasNext() { return cursor.next != list.tail; } public E next() { cursor = cursor.next; return cursor.value; } public void remove() { cursor.prev.next = cursor.next; cursor.next.prev = cursor.prev; list.size--; } } *DriverLabListIterator*
package listDriver;
import list.*; /** * Test Iterators and ListIterators. * */ public class DriverLabListIterator { public static void main() { 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"); }
}