# How to find the length of the array

Jenny raj
Ranch Hand
Posts: 57

In the above program how does the value of a[i].length is 3?
[ October 03, 2005: Message edited by: Barry Gaunt ]

A Kumar
Ranch Hand
Posts: 980
Hi.

this is a 4 X 3 matrix..

which means

a.length gives you 4 ...the no of rows..

[0][0] [0][1] [0][2] <-Now when a[i]..that is individual rows
[1][0] [1][1] [1][2] are scanned...you have columns to store
[2][0] [2][1] [2][2] values.So length is 3
[3][0] [3][1] [3][2]

Hope you got it..

Jenny raj
Ranch Hand
Posts: 57
I still don't understand

are u trying to say that a[i].length gives the length of the column?

Jenny raj
Ranch Hand
Posts: 57
does a[i].length denotes the no of columns?

Akhilesh Trivedi
Ranch Hand
Posts: 1608
Hi jenny!
What is length of an array actually mean, number of elements right???

If "a" is 1-D (One dimensional) array then a.lenth would mean number of elements in the 1-D array.
If a is 2-D array then a[i].length means number of elements, you can have at a[i].
if "a" is 3-D array then a[i][j] means number of elements you can have at a[i][j].

Very very generally speaking, there are no terms like rows or columns... we give the name for our convenience. Hope this helps.

Ranch Hand
Posts: 579
int a[][]=new int[4][3];

Here "int a[][]" OR ("int[][] a") denotes that a is a reference of a "2D array"(due to [][]).And elements (not references )stored actually are of "int" data type.Always visualise array in this very manner.
Now int[][] a=new int[4][3] declares that a is a reference of such a object which stores 4 references of 1-D array and each of which reference such a object having 3 element of int data type.Just see it pictorically.....where references are denoted by dot(.) aand object by box.Hope it clears!

Jenny raj
Ranch Hand
Posts: 57
HI akhil and agrah,thank u for ur neat explanation

Jenny raj
Ranch Hand
Posts: 57
HI akhil and agrah,thank u for ur clear explanation

Junilu Lacar
Bartender
Posts: 8043
64
Java arrays are actually 1-dimensional, just like C (see http://www.eskimo.com/~scs/cclass/int/sx9.html ). You can, however, nest arrays within arrays and this is how you can simulate "multiple" dimensions.