• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding for-each loop

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

can you please explain to me, when to use for-each loop?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you want to loop through something and don't need to change the elements or reference the index.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, when iterating over a Collection and the Iterator isn't needed (for removing elements for instance).
 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne Boyarsky and Rob Prime,
Thanks for your Reply.

And i have one more doubt, Iterator is just a clutter,what it mean?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Java 5 docs:

So when should you use the for-each loop? Any time you can. It really beautifies your code. Unfortunately, you cannot use it everywhere. Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.



In other words: Use the for-each loop whenever you can; only use the old-style loop when it's really necessary.

You can lookup "clutter" with Google Translate.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think they mean that the Iterator is just a temporary thing in most cases. It has exactly one purpose - iterating over the collection. Just like loop counters (i, j, etc) it should only live during the iteration and then be discarded.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

surya.raaj prakash wrote: . . . Iterator is just a clutter,what it mean?

It means you ought not read the website or book where you found that

And please tell us where you found that, so we can also avoid it
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's from the page Jesper linked to - Oracle's own documentation (back then created by Sun).
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just an addition to what the seniors have mentioned.
When you design a class(in this case a collectioin class). Always assume, that even though you as the creator knows everything in it, its internal semantics, to the outside world, it is just a blob. They have (or should have) accecss only in a way which you want them to. In case of collections, you might have a data structure internally, which holds all elements.
}But if you want people to iterate through them, you should have some mechanism.

Foreach (for is a standardized way of iterating. This way you provide the other classes a way of accessing your elements in an order which you specified.
Unfortunately language semantics mess up the whole system if you edit any object on the way. Hence the read only limitation.

the for each loop follows the order specified by your iterator. So if the iterable(and thus the iterator by the iterator defined here) interface is implemented correctly, it should produce a safe output.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:It's from the page Jesper linked to - Oracle's own documentation (back then created by Sun).

No, it isn't. I meant the bit about "iterator is a clutter". There is nothing about clutter in the Java™ Tutorials section.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in short.
we can say for-each loop is same as for loop.
In case of for-each loop, we are only reducing for loop and made the compiler to do some intialization and comparion,assignment by itself.
for-each is most likely used with array.see

if we have a array like
int[] a=new int[]{1,2,3,4,5,6,7,8,9,}

in case of for loop we mention it like this.

for(int i=0;i<=a.length;i++);
System.out.println(a[i]);

but by using for-each loop it can simply be represented as.

for(int i:a)
System.out.println(i);


output will be same for both.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Rob Prime wrote:It's from the page Jesper linked to - Oracle's own documentation (back then created by Sun).

No, it isn't. I meant the bit about "iterator is a clutter". There is nothing about clutter in the Java™ Tutorials section.


Jesper posted this link. Check out the piece of text just after the first code block.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the for each loop allows you the features of iteration , loop includes all the features of previous for loop but has its own enhancements like printing the No of elements in array , is smaller in size , easy to use , proper definition for the developer !!
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

akash shrimali wrote:the for each loop allows you the features of iteration , loop includes all the features of previous for loop but has its own enhancements like printing the No of elements in array , is smaller in size , easy to use , proper definition for the developer !!



No it doesnt..... it is not designed to modify elements which a for loop is.

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Jesper posted this link. Check out the piece of text just after the first code block.

Found it, thank you. It actually says

The for-each construct gets rid of the clutter and the opportunity for error.

. . . which is different from what was originally quoted.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The three sentences before that are

The iterator is just clutter. Furthermore, it is an opportunity for error. The iterator variable occurs three times in each loop: that is two chances to get it wrong.


The first of these three is definitely identical to what was quoted before.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for each always saves your code for throwning uncessary excepion like ArrayIndexoutOfBound Exception
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
search on google the meaning of clutter(if you donot know that)
and try relate it with iterator
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:The three sentences before that are

The iterator is just clutter.

. . .

Even though I used ctrl-F I never found that bit. Sorry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic