-
Enhancement
-
Resolution: Won't Fix
-
P4
-
None
-
5.0
-
sparc
-
solaris_2.5
The compiler may choose foreach loop implementation
with respect to declared collection type.
I proposed the following rule :
if collection is a List that implements RandomAccess,
use indexed loop rather than iterator loop
else use Iterable interface.
In order to convince you, this paragraph is extracted from
RandomAccess documentation :
A List implementation should implement this interface if, for typical
instances of the class, this loop:
for (int i=0, n=list.size(); i < n; i++)
list.get(i);
runs faster than this loop:
for (Iterator i=list.iterator(); i.hasNext(); )
i.next();
with respect to declared collection type.
I proposed the following rule :
if collection is a List that implements RandomAccess,
use indexed loop rather than iterator loop
else use Iterable interface.
In order to convince you, this paragraph is extracted from
RandomAccess documentation :
A List implementation should implement this interface if, for typical
instances of the class, this loop:
for (int i=0, n=list.size(); i < n; i++)
list.get(i);
runs faster than this loop:
for (Iterator i=list.iterator(); i.hasNext(); )
i.next();