Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Multi-Dimensional Arrays

 

A multi-dimensional array  of dimension n (i.e., an n-dimensional array or simply n-D array) is a collection of items which is accessed via n subscript expressions. For example, in a language that supports it, the tex2html_wrap_inline59950 element of the two-dimensional array x is accessed by writing x[i,j].

The Java programming language does not really support multi-dimensional arrays. It does, however, support arrays of arrays. In Java, a two-dimensional array x is really an array of one-dimensional arrays:

int[][] x = new int[3][5];
The expression x[i] selects the tex2html_wrap_inline57340 one-dimensional array; the expression x[i][j] selects the tex2html_wrap_inline59570 element from that array.

The built-in multi-dimensional arrays suffer the same indignities that simple one-dimensional arrays do: Array indices in each dimension range from zero to tex2html_wrap_inline59872, where length is the array length in the given dimension. There is no array assignment operator. The number of dimensions and the size of each dimension is fixed once the array has been allocated.

In order to illustrate how these deficiencies of the Java built-in multi-dimensional arrays can be overcome, we will examine the implementation of a multi-dimensional array class, MultiDimensionalArray, that is based on the one-dimensional array class discussed in Section gif.




next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.