Java Reference
In-Depth Information
The set method changes the data stored in the previously visited element. Its
implementation is straightforward because our linked lists can be traversed in only
on e direction. The linked-list implementation of the standard library must keep track
o f whether the last iterator movement was forward or backward. For that reason, the
standard library forbids a call to the set method following an add or remove
method. We do not enforce that restriction.
676
677
public void set(Object element)
{
if (position == null)
throw new NoSuchElementException();
position.data = element;
}
Finally, the most complex operation is the addition of a node. You insert the new
node after the current position, and set the successor of the new node to the successor
of the current position (see Figure 7 ).
private class LinkedListIterator
implements ListIterator
{
. . .
public void add(Object element)
{
if (position == null)
{
addFirst(element);
position = first;
}
else
{
Node newNode = new Node();
newNode.data = element;
newNode.next = position.next;
position.next = newNode;
position = newNode;
}
previous = position;
}
. . .
}
Search WWH ::




Custom Search