Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How colon : used in for{} loop?

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 is used in a for loop?
Any comments?
JON
[245 byte] By [Joncamp] at [2007-11-11 10:04:32]
# 1 Re: How colon : used in for{} loop?
Hi!

Are u sure it is of the format u provided above? or shown as below:

public class ForeachArray {
public static void main(String args[]) {
String[] data = { "Asia", "Japan" };
for (String s : data) {
System.out.println(s);
}
}
}
abhijava at 2007-11-11 22:32:00 >
# 2 Re: How colon : used in for{} loop?
The enhanced for loop (in reality, an application of the Iterator) allows you to do something with each object in a collection.

The syntax then is to tell the compiler that, for each object in the collection, do something ...

for( String s [THE OBJECT WHICH IS THE CURRENT ELEMENT] : myArrayList [THE COLLECTION])
{
replace( s, a);
}
nspils at 2007-11-11 22:33:01 >