for:each bummer

R

Roedy Green

I just discovered you can't use the new for:each syntax here:

for ( Enumeration e = zip.entries(); e.hasMoreElements(); )
{
ZipEntry entry = (ZipEntry)e.nextElement();

It only works on Iterators (or more precisely that which implements
Iterable.)

I wonder why they left them out?

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
T

Thomas Hawtin

Roedy said:
I just discovered you can't use the new for:each syntax here:

for ( Enumeration e = zip.entries(); e.hasMoreElements(); )
{
ZipEntry entry = (ZipEntry)e.nextElement();

You can write that without casts:

for (
Enumeration<? extends ZipEntry> en = zip.entries();
en.hasMoreElements();
) {
ZipEntry entry = en.nextElement();

It only works on Iterators (or more precisely that which implements
Iterable.)

I wonder why they left them out?

You don't think Enumerators are a bit old for a freshly born language
feature?

As ZipFile is a class rather than an interface, they could add entrySet
or even entryMap to make it work with the enhanced for loop.

Tom Hawtin
 
D

Dale King

Roedy said:
I just discovered you can't use the new for:each syntax here:

for ( Enumeration e = zip.entries(); e.hasMoreElements(); )
{
ZipEntry entry = (ZipEntry)e.nextElement();

It only works on Iterators (or more precisely that which implements
Iterable.)

I wonder why they left them out?

I have no problem with Enumerations not being supported in favor of
iterators, but the reason that iterators are not supported is that they
are one time use only.

Consider this code in your scheme:

Iterator<Object> iter = myCollection.iterator();

for( int i = 0: i < 10; i++ )
{
for( Object o : iter )
{
System.println( o );
}
}

Which will NOT print the contents of the collection 10 times, but only
once. I can see this might potentially confuse a newbie.
 
A

Aleksi Kallio

for ( Enumeration e = zip.entries(); e.hasMoreElements(); )
It only works on Iterators (or more precisely that which implements
Iterable.)
I wonder why they left them out?

The idea was that for(each) is a nice shorthand for the usual cases and
would not support more complicated scenarios. A very generic for(each)
iteration would have naturally been quite complicated, yet maybe not
powerful enough to cover all cases.

I think it sounds quite reasonable...
 
R

Roedy Green

The idea was that for(each) is a nice shorthand for the usual cases and
would not support more complicated scenarios. A very generic for(each)
iteration would have naturally been quite complicated, yet maybe not
powerful enough to cover all cases.

just what CAN you do with it?

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
T

Tor Iver Wilhelmsen

Roedy Green said:
just what CAN you do with it?

Loop over arrays and collections. Which is what you used
iterator()/elements() for earlier. The "for each" statement will get
the iterator for you. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top