JDK-4988624 : New for loop should also accept iterators as their object.
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2004-02-04
  • Updated: 2006-11-02
  • Resolved: 2006-11-02
Related Reports
Duplicate :  
Description
Name: tb29552			Date: 02/04/2004


A DESCRIPTION OF THE REQUEST :
A useful extension to the semantics of the new for loop style

      for (var : Iterable or array)

would be to allow Iterators to be used as the object of the iteration, in addition to arrays and Iterables. Then it would be easy to define alternate iteration styles for a class. For example:

  class MyList {
       Iterator iterator() {...}
       Iterator reverseIterator() {...}
       Iterator evenElementIterator() {...}
   }

  To iterate even elements:

  for (Object element : myListInstance.evenElementIterator()) {...}

JUSTIFICATION :
The current semantics make it awkward to use several types of iteration that a class might wish to provide in a convenient way.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
See Description.
ACTUAL -
See Description.

CUSTOMER SUBMITTED WORKAROUND :
Either
  - Write the for loop the old fashioned way.
  - Define a separate Iterable for each iterator.
(Incident Review ID: 235216) 
======================================================================