# Arrays

mari krishna

Greenhorn

Posts: 10

Thomas Paul

mister krabs

Ranch Hand

Ranch Hand

Posts: 13974

posted 14 years ago

Java doesn't directly support multi-dimensional arrays.

For a two dimensional array you can do something like this:

For a two dimensional array you can do something like this:

Associate Instructor - Hofstra University

Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog

Anonymous

Ranch Hand

Posts: 18944

posted 14 years ago

There's no such thing as 'the length' of a multi dimensional array. Have a look at a one dimensional array:

This array is capable of holding (storing) 42 Objects. But have a look at the following:

This array is capable of holding (storing) 42 arrays of objects. Suppose I do the following:

;

The first 'row' of my multi dimensional array myArray2 contains 4 elements, while the second 'row' of my multi dimensional array myArray2 just contains 2 elements.

Multi dimensional arrays simply don'y exist; arrays of arrays *do* exist though and they're more than convenient. The example above showed a 'ragged' array, i.e. not every 'sub'array contained the same number of elements. The following declaration statement however defines a simple three dimensional matrix though:

Again, this is a 'ragged' array, but all three 'ragged' rows happen to contain the same number of elements, i.e. they all contain three elements and they all happen to be of type 'double'.

The number of rows of such an array (matrix) can be retrieved by the simple expression 'myArray.length', while the number of columns (which happen to be all the same in this particular case) can be retrieved by the simple expression 'myArray3[0].length'.

kind regards

This array is capable of holding (storing) 42 Objects. But have a look at the following:

This array is capable of holding (storing) 42 arrays of objects. Suppose I do the following:

;

The first 'row' of my multi dimensional array myArray2 contains 4 elements, while the second 'row' of my multi dimensional array myArray2 just contains 2 elements.

Multi dimensional arrays simply don'y exist; arrays of arrays *do* exist though and they're more than convenient. The example above showed a 'ragged' array, i.e. not every 'sub'array contained the same number of elements. The following declaration statement however defines a simple three dimensional matrix though:

Again, this is a 'ragged' array, but all three 'ragged' rows happen to contain the same number of elements, i.e. they all contain three elements and they all happen to be of type 'double'.

The number of rows of such an array (matrix) can be retrieved by the simple expression 'myArray.length', while the number of columns (which happen to be all the same in this particular case) can be retrieved by the simple expression 'myArray3[0].length'.

kind regards