aspose file tools
The moose likes Beginning Java and the fly likes Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays" Watch "Arrays" New topic


mari krishna

Joined: Feb 13, 2003
Posts: 10
How to find the length of a multidimensional array.
thanks in advance.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Java doesn't directly support multi-dimensional arrays.
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
It is sorta covered in the JavaRanch Style Guide.
subject: Arrays
It's not a secret anymore!