Taking a Java test I found a for{} loop which had a : character in it?... like this...
for { : }
I was supposed to fill in some objects, but I have no idea how a : character does in a for loop?
Any comments?
JON

Recommended Answers

All 4 Replies

Are you *sure* it wasn't a semi-colon ; ?

no, the new syntax for loop introduced in 5.0 allows an alternative syntax.

for (SomeObject o: someCollection) {
  // do something with o
}

someCollection MUST be of type Collection<SomeObject> OR an array of SomeObject (so of type SomeObject[]).

It is shorthand for creating an Iterator over the Collection and assigning each value returned from the Iterator in turn.

It is similar to the foreach loop in C#, i believe they call it an "advanced for loop" or something.

Us PHP programmers have a foreach ($array AS $value) and foreach ($array AS $key => $value) as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.