• 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

Can using for-loop in place of iterator in case of ArrayList accomplish the same ?

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iterators are used to iterate in java collections but if we specifically take the case of arrayList which has indexes.Does using for loop in place of iterator in case of arralList accomplish the same. We can iterate throught for loop also instead of arrray List.Isnt there any difference in case of arrayList?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot remember the last time I used an iterator.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For loop is useful when you wish to use the index of the element in your collection. Why would you want to use the index? That would depend on your requirement. One far fetched example is a collection of students sorted as per their exam marks. The topper is the 1st element in the collection. The duffer is the last.
Iterators are useful, when you wish to remove an element from your collection without running into the concurrent modification exception.
For everything else use the enhanced for loop, detailed out for you by Bear.
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello !

If you mean foreach loop(by your word "for-loop") then foreach loop internally uses iterator() method.
You hear it right. Foreach loop uses internally iterator() method of collection you provide.
Recall that every class that implements Collection interface also implements Iterable interface.

However using iterator() you can remove elements while iterating by using foreach loop you can't.

If you mean for loop based on index then you can add,remove,replace elements.

To know internal life of ArrayList please cover my Internal life of ArrayList tutorial
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic