Java Reference
In-Depth Information
{
private Node firstNode; // reference to first node
private int numberOfEntries;
public LList()
{
clear();
} // end default constructor
public final void clear() // note the final method
{
firstNode = null ;
numberOfEntries = 0;
} // end clear
< Implementations of the public methods add , remove , replace , getEntry , contains ,
getLength , isEmpty , and toArray go here. >
. . .
// Returns a reference to the node at a given position.
// Precondition: List is not empty;
// 1 <= givenPosition <= numberOfEntries.
private Node getNodeAt( int givenPosition)
{
< See Segment 14.7. >
} // end getNodeAt
private class Node // private inner class
{
< See Listing 3-4 in Chapter 3. >
} // end Node
} // end LList
As we discussed earlier, this version of the class will maintain only a head reference to the chain of
nodes. The data field firstNode is this head reference. Another data field, numberOfEntries , records
the number of entries in the current list. This number is also the number of nodes in the chain. The
default constructor simply 5initializes these data fields by calling clear . So initially, the list is empty,
firstNode is null , and numberOfEntries is 0.
As was noted in Appendix C, when a constructor cnalls another public method such as clear ,
that method should be final so that no subclass can override it, thereby changing the effect of the
constructor. Adding final to clear 's header is an implementation detail that is not reflected in
ListInterface . Recall from Appendix D that an interface cannot declare a method to be final.
VideoNote
The class LList
Adding to the End of the List
14.9
Let's choose the methods add and toArray as the core methods that we will implement first. We
have already defined the method clear , because the constructor calls it.
 
 
Search WWH ::




Custom Search