-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
5.0
-
x86
-
windows_2000, windows_xp
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)
======================================================================
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)
======================================================================
- duplicates
-
JDK-6312085 enhanced-for statement (for-each loop) should support Iterator
-
- Closed
-