-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
5.0, 6
-
x86
-
linux, windows_xp
A DESCRIPTION OF THE REQUEST :
The enhanced for loop introduced in Java 5 requires that the object being looped over be either an array, or a class that implements Iterable. It should also permit an Iterator to be used directly. For example, the following code should be legal:
for (ImageReader reader : ImageIO.getImageReaders(stream))
{
// do something with it
}
Currently, the compiler produces the error message, "foreach not applicable to expression type."
JUSTIFICATION :
There are many classes that provide access to data by means of Iterators, but do not actually implement the Iterable interface. In some cases, this is simply because they were created prior to Java 5. In other cases, the Iterable interface is inappropriate, such as the example above where a class has several methods that each return a different Iterator. It should still be possible to use the enhanced for loop in these cases.
The enhanced for loop introduced in Java 5 requires that the object being looped over be either an array, or a class that implements Iterable. It should also permit an Iterator to be used directly. For example, the following code should be legal:
for (ImageReader reader : ImageIO.getImageReaders(stream))
{
// do something with it
}
Currently, the compiler produces the error message, "foreach not applicable to expression type."
JUSTIFICATION :
There are many classes that provide access to data by means of Iterators, but do not actually implement the Iterable interface. In some cases, this is simply because they were created prior to Java 5. In other cases, the Iterable interface is inappropriate, such as the example above where a class has several methods that each return a different Iterator. It should still be possible to use the enhanced for loop in these cases.
- duplicates
-
JDK-6312085 enhanced-for statement (for-each loop) should support Iterator
-
- Closed
-