Java Reference
In-Depth Information
CHAPTER
4
Grouping objects
Main concepts discussed in this chapter:
collections
loops
iterators
arrays
Java constructs discussed in this chapter:
ArrayList , Iterator , while loop, null , cast, anonymous objects, array,
for loop, for-each loop, ++
The main focus of this chapter is to introduce some of the ways in which objects may be
grouped together into collections. In particular, we discuss the ArrayList class as an example
of flexible-size collections and the use of array objects for fixed-size collections. Closely asso-
ciated with collections is the need to iterate over the elements they contain. For this purpose, we
introduce three new control structures: two versions of the for loop, and the while loop.
This chapter is both long and important. You will not succeed in becoming a good programmer with-
out fully understanding the contents of this chapter. You will need longer to study it than was the case
for previous chapters. Do not be tempted to rush through it; take your time and study it thoroughly.
4.1
Building on themes from Chapter 3
As well as introducing new material on collections and iteration, we will also be revisiting two
of the key themes that were introduced in Chapter 3: abstraction and object interaction . There
we saw that abstraction allows us to simplify a problem by identifying discrete components that
can be viewed as a whole, rather than being concerned with their detail. We will see this prin-
ciple in action when we start making use of the library classes that are available in Java. While
these classes are not, strictly speaking, a part of the language, some of them are so closely
associated with writing most Java programs that they are often thought of in that way. Most
people writing Java programs will constantly check the libraries to see if someone has already
written a class that they can make use of. That way, they save a huge amount of effort that can
be better used in working on other parts of the program. The same principle applies with most
other programming languages, which also tend to have libraries of useful classes. So it pays to
 
 
 
Search WWH ::




Custom Search