Re: What do you think about the for-each loop?



Adam P. Jenkins wrote:
Dale King wrote:

Answering the more general question in the subject, I would like to see and even suggested to Sun that the same construct might be useful for I/O operations.

Consider this loop:

    for( int i; ( i = inputStream.read() ) != -1; )
    {
        byte b = (byte)i;

How about being able to replace that with:

    for( byte b : inputStream )
    {

And you could do a similar thing for reading lines with BufferedReader.


Actually the current foreach construct is already capable of this; inputStream would just need to implement java.lang.Iterable. So this would really be a request for a library enhancement.

That would be a bit inefficient as you would be introducing an unneeded object for every byte that is read.

For me, the main limitation to foreach's usefulness is that it doesn't work for Iterators.

There is a potential of confusion if it worked for iterators. I can see some confusion when you start getting into nested loops.

--
 Dale King
.



Relevant Pages

  • NEWS: Walford welcome new doctor
    ... Not only is there a new doctor about to hit the Square, but they also happen to be young, male and not unpleasant to look at. ... Dr Al Jenkins returns to his East End roots and sets up practice in Walford after leaving the fresh sea air of Ifracombe, ... "Al is single and he likes to mingle", says actor Adam Croasdell, who has previously appeared in The Chase and Holby City. ...
    (rec.arts.tv.uk.eastenders)
  • Re: Compilation Error in Jdk 1.5
    ... "Adam P. Jenkins" wrote: ... I'm curious why that switch is needed. ... comp.lang.java.StringBuffer to implement the String concatenation ...
    (comp.lang.java.programmer)
  • Re: Keep ADAM proxies up-to-date through LDIFDE
    ... and what SUN is doing has been an eye opener for me. ... What more would I like in ADAM? ... can just hope someone in MS is paying attention to customer needs. ... would add to my revious wish list is that ADAMsync needs to be updated ASAP ...
    (microsoft.public.windows.server.active_directory)
  • Re: designer offices
    ... On Sun, 17 Jun 2007 18:50:45 GMT, Adam the Tired wrote in ... I find tremendous irony in the removal of science fiction ... like MS saying "crapware will no longer be available - we would like to ...
    (alt.sysadmin.recovery)
  • Re: Set theory theorem: f_* = g_* iff f^* = g^* iff f = g.
    ... On Sun, 4 Jul 2004, Adam wrote: ... I use regular notation, f^-1for f^*and ffor f_*. ... > of the maps are respectively the same, and so we only need to show the ...
    (sci.math)