Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

which areas have practical implementation of Enhanced for loop.  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unlike general for loop, which areas have the practical implementation/usefulness of the Enhanced for loop...
 
salvin francis
Bartender
Posts: 1591
35
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Have a look at :
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

Quote from there :
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.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say you have an ArrayList:

...and it is filled with elements. You could write:

But yuck, why not just write:

Less code, clearer, one less variable. Win-win!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question should be stood on its head as: when is it appropriate to not use the enhanced for loop?

I cannot remember the last time that I used a traditional for loop construct; one of the few use cases for the traditional form I can think of is when one needs the iteration index within the body of the loop for some reason other than indexing into the collection, and apparently, at least for me, that has not been a need.

Another use case may be if, for some reason, the iteration index needs to be diddled with during the looping process. Though I think I'd have a strong argument that such perplexing code might be better expressed in a less arcane way.
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, now Java8 is here (it is nearly two years old), you would often use a Stream instead of any sort of loop.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Of course, now Java8 is here (it is nearly two years old)


While Java8 may be here, it's not always there; where "there" is what businesses will adopt. None of my clients have plans to move beyond Java7 in the near future. Assuming that Java8 is ubiquitous in business will result in disappointment.
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:unlike general for loop, which areas have the practical implementation/usefulness of the Enhanced for loop...

The most important usefulness or benefit is that by eradicating the loop counter from your code you not only simplify your code as Knute explained but you thereby reduce the risk of a coding error.
One common mistake is to specify the loop termination condition incorrectly, for example you might accidentally write x > 0 instead of x >= 0. Such bugs are not possible in enhanced for loops.
 
Liutauras Vilda
Marshal
Posts: 4635
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beside what all said, still, if you don't feel confident with neither of them, I'd suggest to start with traditional.
Those mistakes (which were mentioned earlier), could cause you a headache, but at the same time will teach you something useful.
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just had the opposite experience.

I have a List of objects that need to be updated whenever
a timer fires. So I used:

However, to my surprise I got a NPE. I knew for sure that words
was not empty and no w was NULL.
Then, after a while, I tried the enhanced for loop:

and after getting a ConcurrentModificationException the penny finally dropped.
(You get the same with words.forEach(w -> w.update(limit))

A ListIterator is good of course, but the plain old simple

is fine.
 
salvin francis
Bartender
Posts: 1591
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
I have a List of objects that need to be updated whenever
a timer fires. ...


I am really surprised that happens in your case... I am under the impression that a concurrent modification happens only when you modify the list (Thus modifying the iterator which is currently in the loop)
here is a program to demonstrate the same:


output:



Does the Word2 class somehow access its iterator ???
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!