[Biojava-l] A question of length?

Matthew Pocock mrp@sanger.ac.uk
Wed, 07 Jun 2000 13:05:00 +0100

Hi Mark,

Mark Schreiber wrote:

> Just a general question about Java so apologies if it doesn't have much to
> do with biojava but...

As good a place as any to ask them...

> the .length member of an array returns the length of the array which is
> very useful, but what is returned if the array is 2 (or more)
> dimensional??

It returns the length of the first dimension

> If it only returns the first dimension then how can you get the length of
> the other dimensions?

Let's say that your code contains the line:

double [][] grid = new double[3][2];
int width = grid.length;
int height = grid[0].length; // 'accidentaly' correct as all cols are the same

When you wrote this, you were thinking of a rectangle of doubles. A C compiler
would more than likely flatten this out into a single array, and then
automagicaly map index pairs onto it. Java, on the other hand, actualy
generates an array of three double [] objects, and then fills each slot with a
double [] of length two.

I could have said:

double [][] mess = new double [3][];
mess[0] = new double[1];
mess[1] = new double[3];
mess[2] = new double[5];
int width = mess.length;
int height = mess[0].length; // wrong! this is only the length of the first

To the vm, there is no way to distinguish between the two cases - a
rectangular block, or an array of disparate elements.

Hope this helps


> thanks
> Mark
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Mark Schreiber                  Ph: 64 3 4797875
> Rm 218                          email mark_s@sanger.otago.ac.nz
> Department of Biochemistry      email m.schreiber@clear.net.nz
> University of Otago
> PO Box 56
> Dunedin
> New Zealand
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> _______________________________________________
> Biojava-l mailing list  -  Biojava-l@biojava.org
> http://biojava.org/mailman/listinfo/biojava-l

Joon: You're out of your tree
Sam:  It wasn't my tree
                                                 (Benny & Joon)