Java Reference
In-Depth Information
When using collections, the need arises to iterate over all elements in a collection to make use
of the objects stored in them. For this purpose, we have seen the use of loops and iterators.
Loops are also a fundamental concept in computing that you will be using in every project from
now on. Make sure you familiarize yourself sufficiently with writing loops—you will not get
very far without them. When deciding on the type of loop to use in a particular situation, it is of-
ten useful to consider whether the task involves definite iteration or indefinite iteration. Is there
certainty or uncertainty about the number of iterations that will be required?
As an aside, we have mentioned the Java class library, a large collection of useful classes that we
can use to make our own classes more powerful. We shall need to study the library in some more
detail to see what else is in it that we should know about. This will be the topic of the next chapter.
Terms introduced in this chapter
collection, array, iterator, for-each loop, while loop, for loop, index, import
statement, library, package, anonymous object, definite iteration, indefinite
iteration
Concept summary
collection A collection object can store an arbitrary number of other objects.
loop A loop can be used to execute a block of statements repeatedly without having to write
them multiple times.
iterator An iterator is an object that provides functionality to iterate over all elements of a col-
lection.
null The Java reserved word null is used to mean “no object” when an object variable is not
currently referring to a particular object. A field that has not explicitly been initialized will contain
the value null by default.
array An array is a special type of collection that can store a fixed number of elements.
Exercise 4.81 In the lab-classes project that we have discussed in previous chapters, the
LabClass class includes a students field to maintain a collection of Student objects.
Read through the LabClass class in order to reinforce some of the concepts we have dis-
cussed in this chapter.
Exercise 4.82 The LabClass class enforces a limit to the number of students who may be
enrolled in a particular tutorial group. In view of this, do you think it would be more appropri-
ate to use a fixed-size array rather than a flexible-size collection for the students field? Give
reasons both for and against the alternatives.
 
Search WWH ::




Custom Search