Java Reference
In-Depth Information
The key characteristic of a search is that it involves indefinite iteration ; this is necessarily so,
because if we knew exactly where to look, we would not need a search at all! Instead, we have
to initiate a search, and it will take an unknown number of iterations before we succeed. This
implies that a for-each loop is inappropriate for use when searching, because it will complete its
full set of iterations. 2
In real search situations, we have to take into account the fact that the search might fail: we
might run out of places to look. That means that we typically have two finishing possibilities to
consider when writing a searching loop:
The search succeeds after an indefinite number of iterations.
The search fails after exhausting all possibilities.
Both of these must be taken into account when writing the loop's condition. As the loop's con-
dition should evaluate to true if we want to iterate one more time, each of the finishing criteria
should, on their own, make the condition evaluate to false to stop the loop.
The fact that we end up searching the whole list when the search fails does not turn a failed search
into an example of definite iteration. The key characteristic of definite iteration is that you can
determine the number of iterations when the loop starts . This won't be the case with a search.
If we are using an index variable to work our way through successive elements of a collection,
then a failed search is easy to identify: the index variable will have been incremented beyond
the final item in the list. That's exactly the situation that is covered in the listAllFiles
method in Code 4.5, where the condition is:
while(index < files.size())
The condition expresses that we want to continue as long as the index is within the valid index range
of the collection; as soon as it has been incremented out of range, then we want the loop to stop. It is
worth pointing out that this condition even works if the list is completely empty. In this case, index
will have been initialized to zero and the call to the size method will return zero too. Because zero
is not less than zero, the loop's body will not be executed at all, which is what we want.
We also need to add a second part to the condition that indicates whether we have found the
search item yet and stops the search when we have. We saw in Section 4.10.1 and Exercise 4.29
that we can often express this positively or negatively, via appropriately set boolean variables:
A variable called searching (or missing , say) initially set to true could keep the search
going until it is set to false inside the loop when the item is found.
A variable called found , initially set to false and used in the condition as !found could keep
the search going until set to true when the item is found.
Here are the two corresponding code fragments that express the full condition in both cases:
int index = 0;
boolean searching = true;
while(index < files.size() && searching)
2 While there are ways to subvert this characteristic of a for-each loop, and they are used quite commonly,
we consider them to be bad style and do not use them in our examples.
Search WWH ::

Custom Search