• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays

 
mari krishna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to find the length of a multidimensional array.
thanks in advance.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java doesn't directly support multi-dimensional arrays.
For a two dimensional array you can do something like this:
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic