Java Reference
In-Depth Information
structures from the Java API's
collection classes
. These offer greater capabilities than tradi-
tional arrays. They're reusable, reliable, powerful and efficient. We focus on the
ArrayList
collection.
ArrayList
s are similar to arrays but provide additional functionality, such as
dynamic resizing
as necessary to accommodate more or fewer elements.
Java SE 8
After reading Chapter 17, Java SE 8 Lambdas and Streams, you'll be able to reimplement
many of Chapter 7's examples in a more concise and elegant manner, and in a way that
makes them easier to parallelize to improve performance on today's multi-core systems.
An array is a group of variables (called
elements
or
components
) containing values that all
have the
same
type. Arrays are
objects
, so they're considered
reference types
. As you'll soon
see, what we typically think of as an array is actually a
reference
to an array object in mem-
ory. The
elements
of an array can be either
primitive types
or
reference types
(including arrays,
as we'll see in Section 7.11). To refer to a particular element in an array, we specify the
name
of the reference to the array and the
position number
of the element in the array. The
position number of the element is called the element's
index
or
subscript
.
Logical Array Representation
Figure 7.1 shows a logical representation of an integer array called
c
. This array contains
12
elements
. A program refers to any one of these elements with an
array-access expression
that includes the
name
of the array followed by the
index
of the particular element in
square brackets
(
[]
)
. The first element in every array has
index zero
and is sometimes
called the
zeroth element.
Thus, the elements of array
c
are
c[0]
,
c[1]
,
c[2]
and so on.
The highest index in array
c
is 11, which is 1 less than 12—the number of elements in the
array. Array names follow the same conventions as other variable names.
c[ 0 ]
-45
Name of array (
c
)
c[ 1 ]
6
c[ 2 ]
0
c[ 3 ]
72
c[ 4 ]
1543
c[ 5 ]
-89
c[ 6 ]
0
c[ 7 ]
c[ 8 ]
c[ 9 ]
c[ 10 ]
c[ 11 ]
62
-3
1
6453
78
Index (or subscript) of the
element in array
c
Fig. 7.1
|
A 12-element array.