aspose file tools*
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
Author

array length?

chinnasamy vijayakumar
Greenhorn

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++)>
{
System.out.println(str.charAt(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...
vijay
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
Rancher

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];
intArray.length
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.
John
[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
Sheriff

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
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: array length?