Java Reference

In-Depth Information

stands for the set of 3 integers
h
,
h+1
, and
h+2
.

h..h+2

stands for the set of 2 integers
h
,
h+1
.

h..h+1

stands for the set of 1 integer,
h
.

h..h

stands for the set of 0 integers.

h..h-1

The last example may seem strange to you, but this convention simplifies array

discussions tremendously. We could have chosen to have
h..h-1
be undefined,

but that would not be as useful.

We often talk about a
segment
of an array: a sequence of adjacent elements

like
b[4]
,
b[5]
,
b[6]
,
b[7]
, using the range notation in the subscript position:

b[4..7]
. To denote a segment
b[h..b.length-1]
, we may omit the last index

and write
b[h..]
.

8.2.2

Horizontal descriptions of arrays.

Instead of a vertical representation of an array, we often use a horizontal version,

as shown below. And we often simplify the picture: we place the name of the

variable directly next to the array and omit the name of the array. (Do not change

your mental picture! An array variable still contains the name of an array object.)

Note that we put the indices of the array elements above the elements, not below.

Activity

8-2.2

0

1

2

3

4

6

-5

2

b

It helps to think of an array as being partitioned into several segments rather

than individual elements. The first segment pictured below is
b[0..3]
; the sec-

ond is
b[4..b.length-1]
. We place a number after a segment boundary (e.g.
4
)

to indicate the subscript of the first element of a segment. We place a number

before the boundary to indicate the last subscript of the segment.

As a final example, we show an array with three segments, with variables

0

3 4

b.length

b

marking the boundaries. The segments are
b[0..i-1]
, the segment consisting of

the individual element
b[i]
, and the segment
b[i+1..]
. If
i
is
3
, the three seg-

ments are
b[0..2]
,
b[3..3]
, and
b[4..]
.

A warning: the picture gives the impression that segment
b[0..i-1]
cannot

0

i

b.length

b

be empty. But if
i
is
0
, the first segment is
b[0..0-1]
, so it is an empty segment.

In this case, the second segment consists of the first element of the array,
b[0]
,

Search WWH ::

Custom Search