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 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,254 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) {

}