Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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. *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"); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Second International Conference Acids Hue City Vietnam March 2010 Proceedings Part 1 Lnai 5990

Authors: Manh Thanh Le ,Jerzy Swiatek ,Ngoc Thanh Nguyen

2010th Edition

3642121446, 978-3642121449

More Books

Students also viewed these Databases questions

Question

6. Identify characteristics of whiteness.

Answered: 1 week ago

Question

e. What are notable achievements of the group?

Answered: 1 week ago