Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

arraylist

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
>
> I would like to know what is the difference between these 2.
>
> ArrayList list = new ArrayList
>
> for(int i=0; i<list.size(); i++){
> //do stuff
> }
>
> Iterator i = list.iterator();
>
> while(i.hasNext(){
> do stuff
> }
>
> Is there performance issues?
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I think that there will be a difference in performance. The iterator code would work faster. It depends on the type of the collection. The first code will force the collection to go and look at the element at a particular index. It is like randomly accessing elements. ArrayList implements the RandomAccess interface meaning that it provides fast random access. So I'm not sure in the case of ArrayList. But in case of LinkedList, I think there will be performance problems with the index based access...
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, Iterator will work fast.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Ankit says, ArrayList provides constant time random access, which means that accessing a random element in the ArrayList by index takes a fixed amount of time and is independent of how many elements the list contains.

If you were using LinkedList instead of ArrayList, then the first method would be less efficient, because a LinkedList needs to walk down the list to find an element by index (it provides linear time random access instead of constant time random access).

Note that this does not mean that your second method of using an Iterator will always be faster for going through the elements of a list. What's most efficient depends on what kind of collection you're going through, how many items are in the collection and other factors. In a real-world situation, you should look at those other factors, and in general you should not micro-optimize your code in advance (based on what you think is more efficient). First write code that works, and if it has a performance problem, then measure (with a profiler) where the bottlenecks are and address those.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abishek Kumar wrote:yes, Iterator will work fast.



for accessing the data for read-only Arraylist will work fast.
Iterator will be used when you have to modify the data while iterating through the list.
Check this link
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now if use the is there difference in performance, too?
 
Ankit Garg
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
camilo the enhanced for loop or for each loop also uses iterator to iterate over a collection. So you can conclude your answer from the above discussion...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic