• Post Reply Bookmark Topic Watch Topic
  • New Topic

which one is the best for retreiving elements from a Collection?  RSS feed

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
which I have to use I mean iterator or enhanced for loop if I want to retrieve elements from a collection and where we have to use iterator and enhance for loop? which one is the best one to use in real time projects?



Thanks in advance.
 
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well when you use enhanced for loop, the compiler actually converts it to a normal for loop with iterator. So actually whether you use enhanced for loop or iterator, both are the same(you cannot modify the collection in both cases otherwise you will get ConcurrentModificationException). It's just that enhanced for loop is more readable when maintaining the code...
 
Rahul Noel
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need more explanation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!