Shiva Mohan
,
Ranch Hand
Apr 24, 2007 09:25:00
Hi ranchers, String s[] = {"s1", "s2","3"}; one array object is created with three references which are refering to values "s1" and "s2" and "3".that is s[0] is refering to "s1",s[1] is refering to "s2" and so on. then how come s[0].length is displaying 2.i thought s[0].length will give only 1 since s[0] refers to "s1". please clarify my doubt.
Meena R. Krishnan
,
Ranch Hand
Apr 24, 2007 09:34:00
s[0].length() means, its the length of the very first element, which in this case is "s1" of two characters width ( s & 1 ). In the following case, in which the first element is an array, [ April 24, 2007: Message edited by: M Krishnan ] [ April 24, 2007: Message edited by: M Krishnan ]
Srinivasan thoyyeti
,
Ranch Hand
Apr 24, 2007 09:35:00
Hi Siva, s[0].length will not compile. 1) length Usage: String s[] = { "Hi" ,"How" ,"are", "you"}; s.length --> 4 // .length applicable on arrays 2) length() Usage: s[0].length() --> 2 // .length() is method defined in String class. s[0].length --> wrong usage // s[0] is not an array instead a String
fred rosenberger
,
lowercase baba
staff
Apr 24, 2007 10:14:00
s[0].length() go through this as follows: s - look at the s array s[0] - look at the 0th element of this array. that happens to refer to a string, as you said, "s1". so now what we have is "s1".length() which is asking for the length of this string. it's 2.
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com