Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Why does fruit[2] = "watermelon" and fruit[3] = "banana"?

At about 9:20 of the video I thought the concept would be fruit[3] = "watermelon" and fruit[4] = "banana" because in the end, there are 4 fruits "apple", "cherry", "watermelon", and "banana". Is it because apple and cherry are set as one string?

I keep repeating the video but I'm still unsure. Reading an explanation will help!

1 Answer

In Java (and most other languages) list indexes are 0 based, meaning that they start at 0, not at 1.

This means that the first item has an index of 0, the second an index of 1, the third an index of 2, and so on. That is why watermelon and banana are one number below where you might expect them to be.

This might seem strange and a bit hard to wrap your head around at first, but 0 based indexes are so common in programming that you will get plenty of practice using them, and they will likely start to become second nature to you soon enough.

Oh yeah! I totally forgot it starts at 0. Okay I got it down now. Thank you again!