aspose file tools*
The moose likes Beginning Java and the fly likes Two-dimensional array lengths???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Two-dimensional array lengths????" Watch "Two-dimensional array lengths????" New topic
Author

Two-dimensional array lengths????

Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
How do I find out the length of a row in a 2-dimensional array. For exanmple if i have [x][y], how do i find the legnth of x? the .length method seems to give me the length of y.

Cheers
[ February 06, 2006: Message edited by: Sam Bluesman ]

Moosey knows best
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

A two-dimensional array in Java is just an array of arrays. Try this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Keep in mind that a 2-d array does not need to be "rectangular," and could contain arrays of different lengths.

For example, if "myArray" represents a 2-d array, then the array at myArray[0] is not necessarily the same length as the array at myArray[1]...

int[][] myArray = { {1}, {2, 3, 4}, {5, 6} };


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Originally posted by marc weber:
Keep in mind that a 2-d array does not need to be "rectangular," and could contain arrays of different lengths.

For example, if "myArray" represents a 2-d array, then the array at myArray[0] is not necessarily the same length as the array at myArray[1]...

int[][] myArray = { {1}, {2, 3, 4}, {5, 6} };


Which also means that :
int[][] myArray = new int[3][];
myArray[0] = new int[1];
myArray[1] = new int[3];
myArray[2] = new int[2];
And you should not forget initialze the arrays
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by memati bas:
...And you should not forget initialze the arrays


Do you mean like this:



There is no emoticon for what I am feeling!
 
Consider Paul's rocket mass heater.
 
subject: Two-dimensional array lengths????