A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Java
»
Java in General
Author
array size
Bill Walker
Greenhorn
Joined: Aug 22, 2002
Posts: 12
posted
Sep 23, 2002 07:48:00
0
Is there a method that returns the number of elements in an array?
Jamie Robertson
Ranch Hand
Joined: Jul 09, 2001
Posts: 1879
I like...
posted
Sep 23, 2002 08:06:00
0
array.length ?
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
Sep 23, 2002 15:16:00
0
String
[] strings = new String[10];
System.out.println(strings.length); // prints 10
Note that length is a property and not a method.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer
-
Blog - Unresolved References
-
Book Review Blog
Shilpa Bhargava
Ranch Hand
Joined: Sep 15, 2002
Posts: 39
posted
Sep 23, 2002 21:49:00
0
Since Arrays are always fixed size, why would one want to use length on array when the size is always already known ??
For
ArrayList
and others it may be reqd ....
Pradeep bhatt
Ranch Hand
Joined: Feb 27, 2002
Posts: 8876
I like...
posted
Sep 23, 2002 23:56:00
0
Consider this:
public void printArray(int [] a)
{
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
The method does not know the length of the array so it uses the field "length" to know the number of elements.
Groovy
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
Sep 25, 2002 22:14:00
0
Also if you change the size of the array then you need to change everywhere you reference it by the hardcoded size.
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: array size
Similar Threads
Doubt
How could I print names randomly?
WA #1.....word association
Another 'Y' game
about array
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter