File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array length? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array length?" Watch "array length?" New topic

array length?

chinnasamy vijayakumar

Joined: Dec 02, 2000
Posts: 8
i want to know the routine in JAVA to compute the length of an array

public class chararray
public static void main(String args[])
String str = "vijayakumar";
System.out.println(" String ----> "+str);
System.out.println(" Length ----> "+str.length());
char mychar[] = new char[str.length()];
for(int i=0 ; i<str.length();i++)>
mychar[i] = str.charAt(i);
for(int i=0;i<getLength(mychar);i++)>
I want to store length of the array in an integer
Above program gives error ...
please correct me...
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
In java to find the length, you use <name of array>.length. Notice there are no parentheses "()" afterwards because there not used. So if I had an array named red, I would write red.length to find out how many elements are inside it.
John Wetherbie

Joined: Apr 05, 2000
Posts: 1449
You can get the length of an array via its length member, i.e.,
int[] intArray = new int[3];
So the for loop could look like:

Hmm, can't seem to get the stuff starting with the less than sign to display. Anyway, the code says i equal 0; i less than the array length; increment i.
[This message has been edited by John Wetherbie (edited December 24, 2000).]
[This message has been edited by John Wetherbie (edited December 24, 2000).]
[This message has been edited by John Wetherbie (edited December 24, 2000).]

[This message has been edited by Ajith Kallambella (edited December 24, 2000).]

The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
John, the code section should end with /code tag. I edited your message to fix the tags. Ofcourse you would enclose the tags in square brackets ([ ] ) and you know why I didn't do that in this message

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
I agree. Here's the link:
subject: array length?
It's not a secret anymore!