guys I have a question in array. When i run the code it prints "sanji" since the statement is [2] & it is count as 0,1,2. now ++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

guys I have a question in array. When i run the code it prints "sanji" since the statement is [2] & it is count as 0,1,2. now ++

if I print the example statement (inside the comment) it will print (3) right? so why did the output is 3 and not 2? or length doesn't read 0's instead it reads 1,2 and so on? Thanks in advance :) https://code.sololearn.com/ciHHoQa88Gj0/?ref=app

5th Oct 2022, 2:58 PM
ivor
ivor - avatar
2 Answers
+ 1
Length means elements present in array. So whenever you count no of elements counting will start from 1 itself. And index always start from 0 length and index both are different things. For example .. In mathematics whole numbers were start from 0 but natural numbers are starts from 1 similarly in case of array as well. This is my explanation or answer. If wrong then kindly correct me
5th Oct 2022, 3:08 PM
Pallavi Jha
Pallavi Jha - avatar
+ 3
String [] anime = { "zoro", "luffy", "sanji" }; anime[ 0 ] = "zoro" anime[ 1 ] = "luffy" anime[ 2 ] = "sanji" There are 3 elements, and index of the elements goes from 0 ~ 2
5th Oct 2022, 3:04 PM
Ipang