File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes size of 2D array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "size of 2D array" Watch "size of 2D array" New topic

size of 2D array

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
Hi guys,
How can I find out length of a 2d array that I get as a parameter of a function?
If I have:
public void getArray(String[][] arr){
How can I find out what is the size of each dimension just by knowing from getArray() that this is a two-dimensional array?

All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24187

Hi Alex,
A Java 2D array is really a 1D array of 1D arrays -- i.e., there's a "backbone" array whose elements are 1D arrays, whose elements are (in your example) Strings. So if the array variable is named "arr", then "arr.length" is one dimension, the length of the backbone. The other dimension can technically be different for every element of the backbone -- the array doesn't have to be "rectangular," it can be "ragged." But if you know that it is rectangular, then the other dimension is just "arr[0].length".

[Jess in Action][AskingGoodQuestions]
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
jQuery in Action, 2nd edition
subject: size of 2D array