Java Reference

In-Depth Information

Java syntax: Partial-array creation

new
type
[
int-expression
][]

Example
:
new int
[n][]

Purpose
: Create only the first dimension of a two-dimensional

array, with
int-expression
elements, all
null
.

Extension
: For an n-dimensional array, any of the first dimen-

sions can be created, e.g.
new int
[5][3][][][]
.

a6

a7

a8

2

4

7

1

3

c a6

a7

a8

Now, row
0
of
c
has
2
elements and row
1
has
3
elements. We call it a
ragged

array
: a two-dimensional array whose columns have different sizes.

In many cases, each row should have the same number of columns, and the

old method of creating both at the same time is the method to use. However, if

you want to save space by having each column have a different number of ele-

ments, use the new method, as just illustrated.

9.3.2

Pascal's triangle

In order to show one use of ragged arrays, we introduce Pascal's triangle, attrib-

uted to Blaise Pascal, a Swiss mathematician and philosopher, who first discov-

ered this little triangle and its properties. Below, we show Pascal's triangle in the

middle. On the right, we show it as we usually draw a two-dimensional array:

row 0

1

1

row 1

1 1

1 1

row 2

1 2 1

1 2 1

row 3

1 3 3 1

1 3 3 1

row 4

1 4 6 4 1

1 4 6 4 1

row 5

1 5 10 10 5 1

1 5 10 10 5 1

...

...

...

Each row
r
of this triangle contains
r+1
integers. The first and last elements of

each row are
1
. Each other element
t[r][c]
is the sum of the two elements

above it:

t[r][c] = t[r - 1][c - 1] + t[r - 1][c] (
for
0<r
,
0<c<r)

Pascal's triangle and combinatorics

Pascal's triangle is important in the field called
combinatorics
. The integer

Search WWH ::

Custom Search