Java Reference
In-Depth Information
11.
private
Node getReferenceTo(T anEntry)
{
boolean
found =
false
;
Node currentNode = firstNode;
int
counter = 0;
while
(!found && (counter < numberOfEntries))
{
if
(anEntry.equals(currentNode.data))
found =
true
;
else
{
currentNode = currentNode.next;
counter++;
}
// end if
}
// end while
return
currentNode;
}
// end getReferenceTo
12.
The original definition of
getReferenceTo
ensures that
currentNode
is not
null
, and thus avoids a
NullPointerException
.
13.
The effort expended by each of these two methods is about the same. Each method calls a private method
that searches for the desired entry. In
LinkedBag
,
contains
calls
getReferenceTo
, which searches at most
numberOfEntries
nodes for the desired entry. In
ResizableArrayBag
,
contains
calls
getIndexOf
, which
searches at most
numberOfEntries
array elements for the desired entry. The next chapter will discuss these
methods and analyze their time requirements in more detail.