Java Reference

In-Depth Information

Chapter
9

Multi-dimensional Arrays

OBJECTIVES

• Learn how to create and use a rectangular array.

• Learn about ragged arrays of any dimension.

INTRODUCTION

Two-dimensional tables and the mathematical
matrix
can be implemented using

rectangular arrays. However, although we may abstractly think in terms of a rec-

tangular array, Java implements it in a way that allows rows of a rectangular array

to have different lengths, giving us the so-called
ragged array
. In this chapter, we

use what you have already learned about arrays in the previous chapter to show

how to create and manipulate ragged arrays.

9.1

Rectangular arrays

Consider the following declaration and assignment statement:

int
[][] b;

b=
new int
[2][3];

These differ from the declaration and assignment for a one-dimensional array in

that there are two pairs of brackets, not one. This means that
b
contains the name

of a two-dimensional array, or table, and not a one-dimensional array:

a0

2 5 4

1 4 8

b
a0

Search WWH ::

Custom Search