Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# How to find the length of the array

Jenny raj
Ranch Hand

Joined: May 19, 2005
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

Joined: Jul 04, 2004
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

Joined: May 19, 2005
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

Joined: May 19, 2005
Posts: 57
does a[i].length denotes the no of columns?
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1599
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.

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
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

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

Joined: May 19, 2005
Posts: 57
HI akhil and agrah,thank u for ur clear explanation
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 6529

21

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.

See also: http://java.sun.com/docs/books/tutorial/java/data/multiarrays.html
[ October 05, 2005: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]

I agree. Here's the link: http://aspose.com/file-tools

subject: How to find the length of the array