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

enhanced-for statement (for-each loop) should support Iterator

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 5.0, 6
    • specification

      A DESCRIPTION OF THE REQUEST :
      Currently, the for/in statement (ie for(Date date : collection)...) only supports Iterable and Object[] as the source of objects to iterate over. There is no reason why this statement should not support Iterators directly. For instance, the following code should work

      List<Integer> myList = buildIntegers();
      for (Integer integer : myList.iterator())
          System.out.println(integer);

      JUSTIFICATION :
      Sometimes I will have a method other than iterator() that returns the iterator I want to travers. For instance, I could have a method that returns an iterator over a subset of objects in the source object...

      Iterator<Integer> getOddIntegers();

      Right now, I'm forced to use old style for loops with iterators. I'd like to be able to type

      for (Integer integer : myList.getOddIntegers())
         System.out.println(integer);

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      If I pass an iterator as the argument to a for/in loop after the colon, it should just work.
      ACTUAL -
      I get a compile error when I try this now

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: