This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Iterating arraylist over arrays  RSS feed

 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having an arraylist which contains String objects(about 200-300). and i have to iterate it. Which is better iterating an arraylist or converting arraylist to object array and than iterating that object array. or its same both ways
thyanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The two choices really are
  • Using an Iterator, or
  • Using a for loop with size() and get().


  • The overhead of iteration is going to be very small no matter how you do it. 200-300 items is enough that the one-time startup costs of creating an Iterator won't matter, and small enough that any method-call overhead won't add up to much, either. The Iterator is more idiomatic (i.e., better style) so I would just use that.
    In any event, converting an ArrayList to an array and then iterating over it never makes any sense; you'd got the overhead of creating the large array, and then copying the old data into the new array. This last part, especially, will be much more expensive than just visiting each item in the ArrayList once, since you'll be visiting each item in two different arrays once just for the copying, and then one of them one more time for the iteration.
     
    Prakash Dwivedi
    Ranch Hand
    Posts: 452
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks Ernest,
    now i have another question. for iterating arraylist. as u said that there are two options and using iterator is better than for loop(using size() and get). But is using iterator still better if i use for loop like this:
    int listSize = list.size();
    for(int i=0;i<listSize;i++){
    Object o = list.get(i);
    }
    Here i am not invoking size() in each iteration. Also in my case there is no possibility that the size of the list changes during this for loop.
    Plz comment
    thanks
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, EHF's comments still apply; the overhead of size() is only a small part of what he was talking about.
    I disagree with EFH slightly here - using get() is fairly common, and an be a good idea, but you have to be sure of what you're doing. Using get(i) will be slightly faster than an Iterator if you're absolutely sure the List you're dealing with is an ArrayList. However if you ever replace it with a LinkedList, using get() becomes a very bad idea, while using an Iterator is very fast. So basically, if you you Iterator when get() would be faster (ArrayList), the penalty is minor; if you use get() when Iterator would be faster (LinkedList) the penalty is severe. Thus, the Iterator is recommended as the best general solution. The get() may be used, but only use it if you're sure that you're dealing with an ArrayList and that you won't change your mind later and forget to change the get() code.
    Note that I never referred to a for() loop above; that's because I use a for loop for both the get() and the Iterator solution. E.g. to use an Iterator:

    Or to use get():

    Again, the get() solution will be slightly faster for an ArrayList, but the difference is usually minor, especially if the loop actually does anything (if doSomethingWith() is not extremely short and simple). The Iterator solution is only a little worse for ArrayList, and is much, much better for LinkedList, so perfer this solution if there's any chance you might switch to LinkedList later.
    Finally, if you have a List of unknown tpe, and really want to iterate as fast as you possibly can, there's this:

    The RandomAccess interface was introduced in JDK 1.4 for precisely this type of usage - it identifies those classes for whom an index get() is faster than an Iterator. (Vector is the other class like this.) Obviously code like this is more work to write, but in some cases it's justified. See the source code to Collections.java (a version from JDK 1.4+) for many examples - they're writing code that will be used by many people for many differnent types of Collections, so they took the extra time to optimize it.
    Also, note that JDK 1.5 will have a new wayt of accomplishing this:

    This will surely become the preferred way to loop through lists - the compiler will generate code equivalent to the last "fully optimized" version I showd above, but you wont have to look at it or think about it. Or type it.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!