Java Reference
In-Depth Information
interface in Listing 15-8 that declares the operations of the ADT list and the method
getIterator
.
In this case, the method's return type is
ListIterator<T>
instead of
Iterator<T>
.
LISTING 15-8
The interface
ListWithListIteratorInterface
import
java.util.ListIterator;
public
interface
ListWithListIteratorInterface<T>
extends
ListInterface<T>
{
public
ListIterator<T> getIterator();
}
// end ListWithListIteratorInterface
15.42
The class that implements the ADT list.
Our class has the same data fields and methods as the
class
AList
given in Chapter 13, and includes the method
getIterator
. The class also contains the
inner class
IteratorForArrayList
, which implements the interface
ListIterator
. Listing 15-9
shows the form of our new class of lists.
LISTING 15-9
An outline of the class
ArrayListWithListIterator
import
java.util.ListIterator;
import
java.util.NoSuchElementException;
public
class
ArrayListWithListIterator<T>
implements
ListWithListIteratorInterface<T>
{
private
T[] list;
// array of list entries
private
int
numberOfEntries;
private
static
final int
DEFAULT_INITIAL_CAPACITY = 25;
public
ArrayListWithListIterator()
{
this
(DEFAULT_INITIAL_CAPACITY);
}
// end default constructor
public
ArrayListWithListIterator(
int
initialCapacity)
{
numberOfEntries = 0;
// the cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempList = (T[])
new
Object[initialCapacity];
list = tempList;
}
// end constructor
< Implementations of the methods of the ADT list go here;
you can see them in Chapter 13, beginning at Segment 13.5.>
. . .