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 Java Data Structures - Retired Organizing Data Review Arrays

Jan Gooßens
Jan Gooßens
22,344 Points

Array Quiz

String[] jacksons = new String[5];

jacksons[0] = "Michael";

jacksons[1] = "Tito";

What is the result of the expression jacksons.length ?

The answers are 2 or 5.

But i think it has to be 6 because of the start from zero ? Right?

1 Answer

andren
andren
28,558 Points

While I can certainly see why you would think so the answer is in fact 5.

When you create a new array the number you put in the brackets is the number of objects you want to create. So 5 objects are created in the array at the start. Since the length property counts the number of objects (not their index) it will return 5 when there are 5 objects.