I came across some Java syntax that I haven’t seen before. I was wondering if someone could tell me what’s going on here.
for (ObjectType objectName : collectionName.getObjects())
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s called a for-each or enhanced
forstatement. See the JLS §14.14.2.It’s syntactic sugar provided by the compiler for iterating over
Iterables and arrays. The following are equivalent ways to iterate over a list:and these are two equivalent ways to iterate over an array:
Further reading