Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6360734

Enhanced for loop should support iterators directly

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 5.0, 6
    • specification
    • 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.

            abuckley Alex Buckley
            jssunw Jitender S (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: