This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Can using for-loop in place of iterator in case of ArrayList accomplish the same ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can using for-loop in place of iterator in case of ArrayList accomplish the same ?" Watch "Can using for-loop in place of iterator in case of ArrayList accomplish the same ?" New topic
Author

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

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 586
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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

I cannot remember the last time I used an iterator.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

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


True person is moral, false is right!
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 586
thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can using for-loop in place of iterator in case of ArrayList accomplish the same ?
 
Similar Threads
is using ArrayList.iterator() is faster than looping ArrayList in for loop?
problem with advance for loop
help me get out of this loop problem
Iteration speed of Collections
iterator method of ArrayList class