Java Reference
In-Depth Information
A limitation of using strings
Of course, by this point we can see that simply having file name strings containing all the de-
tails of the music track is not really satisfactory. For instance, suppose we wanted to find all
tracks with the word “love” in the title. Using the unsophisticated matching technique described
above, we will also find tracks by artists whose names happen to have that sequence of char-
acters in them (e.g., Glover). While that might not seem like a particularly big problem, it does
have a rather “cheap” feel about it, and it should be possible to do better with just a little more
effort. What we really need is a separate class— Track , say—that stores the details of artist and
title independently of the file name. Then we could more easily match titles separately from art-
ists. The ArrayList<String> in the organizer would then become an ArrayList<Track> .
As we develop the music-organizer project in later sections, we will eventually move towards a
better structure by introducing a Track class.
Summary of the for-each loop
The for-each loop is always used to iterate over a collection. It provides us with a way to access
every item in the collection in sequence, one by one, and process those items in whatever way
we want. We can choose to do the same thing to each item (as we did when printing the full
listing) or we can be selective and filter the list (as we did when we printed only a subset of the
collection). The body of the loop can be as complicated as we like.
With its essential simplicity necessarily come some limitations. For instance, one restriction is
that we cannot change what is stored in the collection while iterating over it, either by adding
new items to it or removing items from it. That doesn't mean, however, that we cannot change
the states of objects already within the collection.
We have also seen that the for-each loop does not provide us with an index value for the items
in the collection. If we want one, then we have to declare and maintain our own local variable.
The reason for this has to do with abstraction, again. When dealing with collections and iterat-
ing over them, it is worth bearing two things in mind:
A for-each loop provides a general control structure for iterating over different types of collection.
There are some types of collections that do not naturally associate integer indices with the
items they store. We will meet some of these in Chapter 5.
So the for-each loop abstracts the task of processing a complete collection, element by element,
and is able to handle different types of collection. We do not need to know the details of how it
manages that.
One of the questions we have not asked is whether a for-each loop can be used if we want to stop
partway through processing the collection. For instance, suppose that instead of playing every
track by our chosen artist, we just wanted to find the first one and play it, without going any fur-
ther. While in principle it is possible to do this using a for-each loop, our practice and advice is not
to use a for-each loop for tasks that might not need to process the whole collection. In other words,
we recommend using a for-each loop only if you definitely want to process the whole collection .
Again, stated in another way, once the loop starts, you know for sure how many times the body
will be executed—this will be equal to the size of the collection. This style is often called definite
Search WWH ::

Custom Search