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 Arrays

dexter cachola
dexter cachola
413 Points

for (int i = 0; i < jacksons.length; i++) { why did craig needed to add the .length method?

why did craig needed to add the .length method?

i tried removing the .length method just and have the same results

thank you

3 Answers

When using arrays, some java versions donโ€™t infer the length of an array; so is to best practice to place the .length whenever you want to get the length of an array.

Craig used the .length method to loop until the array comes to an end or until the last entry. This practice is i think very useful when dealing with an array containing unknown no of elements containing in that particular array. In this way we don't actually need to specify the ending point, .length method does that for us

Quinton Rivera
Quinton Rivera
5,177 Points

He didnt have to he just want to show you the method

a lot of time you will use the length method to stop loop

If you dont know many times you have to do something set the termination condition in the for loop to to i < array.length, so you can do everything all at once

instead of locating the number of items then set that value as your loop terminator