Re: What do you think about the for-each loop?
- From: Dale King <DaleWKing@xxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 19 Apr 2005 15:47:54 GMT
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 .
- Follow-Ups:
- Re: What do you think about the for-each loop?
- From: Adam P. Jenkins
- Re: What do you think about the for-each loop?
- References:
- What do you think about the for-each loop?
- From: Red Orchid
- Re: What do you think about the for-each loop?
- From: Dale King
- Re: What do you think about the for-each loop?
- From: Adam P. Jenkins
- What do you think about the for-each loop?
- Prev by Date: Re: Running JSPs on Websphere
- Next by Date: Re: Don't like JSTL
- Previous by thread: Re: What do you think about the for-each loop?
- Next by thread: Re: What do you think about the for-each loop?
- Index(es):
Relevant Pages
|