Java Reference
In-Depth Information
4.16
Fixed-size collections
Flexible-size collections are powerful both because we do not need to know in advance how many
items will be stored in them and because it is possible to vary the number of items they hold. However,
some applications are different in that we do know in advance how many items we wish to store in a
collection, and that number typically remains fixed for the life of the collection. In such circumstances,
we have the option of choosing to use a specialized fixed-size collection object to store the items.
A fixed-size collection is called an array. Although the fixed-size nature of arrays can be a
significant disadvantage in many situations, they do have at least two compensating advantages
over the flexible-size collection classes:
Concept:
An array is a spe-
cial type of collec-
tion that can store
a fixed number of
items.
Access to the items held in an array is often more efficient than access to the items in a com-
parable flexible-size collection.
Arrays are able to store either objects or primitive-type values. Flexible-size collections can
store only objects. 4
Another distinctive feature of arrays is that they have special syntactic support in Java; they can
be accessed using a custom syntax different from the usual method calls. The reason for this
is mostly historical: arrays are the oldest collection structure in programming languages, and
syntax for dealing with arrays has developed over many decades. Java uses the same syntax es-
tablished in other programming languages to keep things simple for programmers who are used
to arrays already, even though it is not consistent with the rest of the language syntax.
In the following sections, we shall show how arrays can be used to maintain collections of fixed
size. We shall also introduce a new loop structure that is often closely associated with arrays—
the for loop. (Note that the for loop is different from the for-each loop. )
4.16.1
A log-file analyzer
Web servers typically maintain log files of client accesses to the web pages that they store.
Given suitable tools, these logs enable web service managers to extract and analyze useful in-
formation such as:
which are the most popular pages they provide
which sites brought users to this one
whether other sites appear to have broken links to this site's pages
how much data is being delivered to clients
the busiest periods over the course of a day, a week, or a month
Such information might help managers to determine, for instance, whether they need to
upgrade to more-powerful server machines or when the quietest periods are in order to schedule
maintenance activities.
4 A Java construct called “auto-boxing” provides a mechanism that also lets us store primitive values in
flexible-size collections. It is, however, true that only arrays can directly store them.
 
 
Search WWH ::




Custom Search