• Post Reply Bookmark Topic Watch Topic
  • New Topic

confused abut Enhanced For loop  RSS feed

 
Rameez Shaik
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



i am not able to understand the part

here we haven't initialize and increment the item in the syntax , then how cm it know to increase the item by one?? lots of confusion
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

here we haven't initialize and increment the item in the syntax

Yes, you have. What this code is basically saying is "for each 'item' (of the type int) in the collection 'numbers' do ..." etc. You don't need to increment the variable because it does not represent the index you access an array with, it is assigned the value of each element in the array as the loop reaches it.

 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It loops through collection specified- element by element. I dont know if its really complicated or you want to know in depth about it- how its implemented. You can may be compare the byte codes - in ordinary loop and the enhanced loop case.
 
Rameez Shaik
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when can enhanced for loop can be used ??

we can use it only when we are working with collection of similar data types i.e array ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can be used on every Iterable, as well as simple arrays.
 
Nipun Arora
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use for-each loop for both arrays and collection.

The enhanced for loop was mainly introduced to help traversing a collection in a short and neat code, replacing the Iterator which is used for the same purpose.

Now with enhanced for loop, you can replace the last while loop with the following code:

No need for Iterator. But you can't simply replace a for loop with a for-each loop, since for-each loop traverses each and every index in the order whereas in for loop you can set the increment step value.
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nipun Arora wrote: But you can't simply replace a for loop with a for-each loop, since for-each loop traverses each and every index in the order whereas in for loop you can set the increment step value.
And not just the step value, but also a stop value. As you say, the intent of the for-each loop is to traverse all of the collection, not just a portion thereof. To the OP: this is partly a matter of nomenclature. Calling it an "enhanced for loop" is correct, but calling it a "for-each" loop tells you what it does.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nipun Arora wrote: . . . No need for Iterator. . . .
I thought the for-each loop calls an Iterator when traversing an Iterable. It is syntactic sugar to make it easier to write.
 
Nipun Arora
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Nipun Arora wrote: . . . No need for Iterator. . . .
I thought the for-each loop calls an Iterator when traversing an Iterable. It is syntactic sugar to make it easier to write.

Does for-each loop calls iterator only internally? didnt know that ... ! Anyways, by "No need for Iterator" I meant coder doesnt hav to declare Iterator object and can straight away work with for loop...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!