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,656 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