for:each bummer

Discussion in 'Java' started by Roedy Green, Aug 3, 2005.

  1. Roedy Green

    Roedy Green Guest

    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
    Roedy Green, Aug 3, 2005
    #1
    1. Advertising

  2. Roedy Green wrote:
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Aug 3, 2005
    #2
    1. Advertising

  3. Roedy Green

    Dale King Guest

    Roedy Green wrote:
    > 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.
    --
    Dale King
    Dale King, Aug 3, 2005
    #3
  4. > 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...
    Aleksi Kallio, Aug 3, 2005
    #4
  5. Roedy Green

    Roedy Green Guest

    On Wed, 03 Aug 2005 09:16:37 +0300, Aleksi Kallio
    <> wrote or quoted :

    >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
    Roedy Green, Aug 3, 2005
    #5
  6. Roedy Green <> writes:

    > 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. :)
    Tor Iver Wilhelmsen, Aug 3, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    394
    Eliyahu Goldin
    Aug 3, 2005
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    412
    Tjerk Wolterink
    Nov 3, 2004
  3. Pat Maddox
    Replies:
    6
    Views:
    133
    Marcin Mielżyński
    Jan 20, 2006
  4. Igor Nn
    Replies:
    7
    Views:
    423
    Johnny Morrice
    May 28, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    115
    PerlFAQ Server
    Jan 26, 2011
Loading...

Share This Page