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

How the heck do for loops in Java work?

I'm working on a chapter in the Java course and I just don't understand this code:

for (BlogPost post : mPosts)

It seems to me that this was a notation designed in Java to handle arrays. The first part of it is the initialization of the counter variable, typically an int, and the second part the array or list to be iterated through.

So how does this work with the above code? Someone posted it on the chapter I'm working on in Java data structures for the code challenge, but that literally makes zero sense to me. BlogPost post would initialize a new type of BlogPost, then mPosts would take the member variable that stores all of the posts in a list and iterate them with...a Blog Post?

What the heck is going on there?

4 Answers

Michael Norman
PLUS
Michael Norman
Courses Plus Student 9,399 Points

In this example mPosts may be an array of BlogPost objects and "post" is an uninitialized BlogPost object. The loop is saying, for each item in mPosts (which is an array of BlogPost objects) assign it to post (a BlogPost object) so that we can do something with it in the loop. Each iteration it assigns the next item from mPosts to the post object.

This particular loop is commonly called a for-each loop, please look here for more info for-each loop.

I know the explanation was kind of poor, but hopefully it is somewhat helpful.

Still not making sense.

The above code snippet is from this challenge:

https://teamtreehouse.com/forum/java-help-7

I don't want to move on til I have a firm understanding of this concept.

Wait, I'm starting to get it.

So it uses the uninitialized value post to define a specific post each time it goes through the for loop and runs the code?

Then it runs whatever code comes at the end of the phrase until you run out of values in mPosts to put into the posts value?

HOW THE HELL DOES JAVA KNOW HOW TO DO ALL THAT WITH SO LITTLE CODE?!?!?!!

Greg Kaleka
Greg Kaleka
39,021 Points

Hah - hey Eric. Sounds like you understand it exactly. Programming is pretty cool, huh?!

For loops are extremely common, and yes, very powerful. They're implemented as a function - note the way they're written: functionName(parameters).

Because they're baked right into the language, you can just call them, and Java has a set of instructions for how to run a for loop, which it follows. Just like you could create a very complex function called, say, calculateRocketTrajectory, which did a million calculations, and then call it somewhere else in your code with one line, for loops can be called with a tiny amount of code.

Make sense?

It does, except now I feel like this stuff has been created by wizards reading my mind.