Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java For Each

Thomas Rossini
Thomas Rossini
544 Points

for "each" ":" ?

does this symbol represent "each" hence ... for (String jackson search each jacksons) " send to method" console.printf("%s Jackson %n", jackson); ............... also shouldnt jackson in the method be jacksons so it recognizes the array name ?

1 Answer

Brendon Butler
Brendon Butler
4,242 Points

Yes it represents each. The "search" term is not part of this loop. It essentially translates to "for each in jacksons."

To your second question -- No. When using a for loop you need a way to differentiate between your array and a specific variable in the array.

Basically what you're doing is every time the loop runs it assigns the next variable inside the "jacksons" array to it's own variable "jackson".

That way instead of having to keep a running integer of which variable you're accessing, then using that integer to retreive your variable, it's all done for you within the loop.

for (int i = 0; i < jacksons.length(); i++) {
    String jackson = jacksons.get(i);
}
// can be simplified to
for (String jackson : jacksons) {

}