This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array length?

 
chinnasamy vijayakumar
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 625
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1449
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic