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 Exploring the Java Collection Framework Sets

Shezan Kazi
Shezan Kazi
10,807 Points

Explaination of for statement

Hi,

I regularly see a statement of this sort: for (Treet treets : treet)

Could someone explain what this means? Thanks!

1 Answer

Nico Julian
Nico Julian
23,657 Points

Hi,

This is an example of an enhanced for-loop, or a for-each loop.

for (Treet treet : treets)

The first capitalized Treet is the data type of the treet that follows. The : can be read as "in", and is followed by the array that will be iterated over. It basically means for each treet of type Treet in the array called treets.

for (Treet treet : treets) {
     System.out.println(treet);
}

For example, this piece of code will take each treet of type Treet in the array, treets, and print it.

Hope that helps. Here's a link to a bit more on enhanced for-loops.

https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with

Shezan Kazi
Shezan Kazi
10,807 Points

Hi Nico,

thank you very much. This is exactly the explaination I was hoping to get.

Cheers, Shezan