0

I'm trying to figure out what this is actually called and where I can find the answer for it. I can't understand the difference in

for (Treet treet : treets) { }

I don't know what the different "treet" mean. can someone help me or at least tell me what it's called?

3
  • 2
    Bad question title. It's a down-vote and close question vote magnet, so please change it soon. Commented Nov 2, 2015 at 23:47
  • 3
    Enhanced for statement Commented Nov 2, 2015 at 23:48
  • @Andy Turner, thank you for the edit. Commented Nov 2, 2015 at 23:50

2 Answers 2

2
for (Treet treet : treets) { }

This is an enhanced for statement (although often referred to as an enhanced for loop). You are saying that you want to do something for every element in some collection of things. (Well, actually, you're doing nothing here, but never mind).

In English: "For every Treet in treets, do something".

Treet is a type. Somewhere you need a class called Treet defined. If it's in a different package, you need to import it.

treets is either an Iterable<? extends Treet>, meaning you could call treets.iterator() and use the result in the standard hasNext()/next() way; or an array of something which extends Treet. It's something you can iterate through.

treet is a single element from the iterable/array. You can use it only inside the body of the loop.

Sign up to request clarification or add additional context in comments.

3 Comments

So how would it be read if I were saying it out loud in English?
@Downvoter: care to explain? I can understand why somebody new to Java might find it confusing.
Thank you Andy Turner. I wasn't trying to be a bother, I just know that StackOverfow usually has a community of people that understand and are helpful to those of us that have a little less knowledge than others. Thank you for all of your help.
0

Thats a forach loop, it iterates through the array/list/... and performs the actions defined in the loop to every element in the list which is referenced by treet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.