tags:

views:

883

answers:

7

Hi, I have a 2D array of doubles in Java which is basically a table of values and I want to find out how many rows it has...

It is declared elsewhere (and allocated) like this:

double[][] table;

then passed to a function...

private void doSomething(double[][] table)
{

}

In my function I want to know the length of each dimension without having to pass them around as arguments. I can do this for the number of columns but don't know how to do it for the rows...

int cols = table[0].length;
int rows = ?;

How do I do that?

Can I just say...

int rows = table.length;

Why would that not give rows x cols?

+2  A: 

Look at this example. They use .length there to get the row count, [].length to get the column count.

It's because the array consists of "row" pointers and these could lead anywhere, so you have to use .length for each column to get the sizes of these arrays, too.

schnaader
+15  A: 

In Java a 2D array is nothing more than an array of arrays.

This means that you can easily get the number of rows like this:

int rows = array.length;

This also means that each row in such an array can have a different amount of elements (i.e. each row can have a varying number of columns).

int columnsInFirstRow = array[0].length;

This will only give you the number of columns in the first row, but the second row could have more or less columns than that.

You could specify that your method only takes rectangular arrays and assume that each row has the same number of columns than the first one. But in that case I'd wrap the 2D-array in some Matrix class (that you might have to write).

This kind of array is called a Jagged Array.

Joachim Sauer
A: 

No it won't. You can just use table.length

tehvan
+1  A: 

I don't quite understand your question. You have to realize that in Java, multidimensional arrays are in fact arrays of arrays.

table.length will in fact give you the number of one-dimensional arrays contained in your two-dimensional array (assuming there are no null entries).

table[0].length will give you the size of the first one-dimensional entry. This could be different from the size of the others, and it could throw a NullPointerException (though neither is possible when it's allocated as new double[rows][cols]).

Michael Borgwardt
+1  A: 

Well, first of all, why don't you just test if

int rows = table.length;

gives the desired result?

It does indeed give you the number of elements in the first dimension. Multidimensional arrays are in fact nothing special, as they are just arrays of arrays of arrays of ... In your case - a 2D array - it is organized like this (in pseudo-code):

double[][] foo = { double[] = {...}, double[] = {...}, ... }

So when accessing foo, it refers to the "outer" array, which has of course the length property, which contains the number of arrays it contains on the second level.

You can read the double[][] as "An array ([]) of double-arrrays (double[])".

Simon Lehmann
A: 

Alternatively, you could wrap your 2d array into a Matrix or Grid class of some sort. Internally, you could represent it as a 1d array and compute offsets for the row/col coordinates.

Wouter Lievens
+1  A: 

See a nice explanation here.