Java Reference
In-Depth Information
{
nodeBefore = currentNode;
currentNode = currentNode.getNextNode();
} // end while
if ( (currentNode != null ) && key.equals(currentNode.getKey()) )
{
result = currentNode.getValue();
currentNode.setValue(value); // replace value
}
else
{
Node newNode = new Node(key, value); // create new node
numberOfEntries++; // increase length for both cases
if (nodeBefore == null )
{ // add at beginning (includes empty chain)
newNode.setNextNode(firstNode);
firstNode = newNode;
}
else // add elsewhere in non-empty chain
{
newNode.setNextNode(currentNode); // currentNode is after new
// node
nodeBefore.setNextNode(newNode); // nodeBefore is before new
// node
} // end if
} // end if
return result;
} // end add
< Implementations of the other methods in DictionaryInterface >
. . .
< Private classes KeyIterator and ValueIterator (See Segment 20.26) >
. . .
< The private class Node >
. . .
} // end SortedLinkedDictionary
20.25
Iterators. As we mentioned in Segment 20.20, iterators provide the client with an easy way to traverse
a dictionary's search keys and their corresponding values. The public methods getKeyIterator and
getValueIterator have the same implementations here as they do in SortedVectorDictionary . The
private inner classes KeyIterator and ValueIterator , however, differ. Each has a data field nextNode
to mark an iteration's place in the chain as the traversal progresses. Listing 20-6 shows the private class
Search WWH ::




Custom Search