• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array length?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic