# size of 2D array

Alex Kravets

Ranch Hand

Posts: 476

posted 12 years ago

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?

thanks,

Alex

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?

thanks,

Alex

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

posted 12 years ago

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".

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".