Iterator vs ArrayList.get(int index)
I am wondering what's the difference between accessing objects from an ArrayList using iterator() and get(int index). Is there any advantages from using either the iterator() method or the get(int index) method?ThanksMessage was edited by: Plee
[273 byte] By [
Pleea] at [2007-11-27 7:57:18]

If the ArrayList has 1000 elements and you only want the last one, obviously ArrayList.get(999) will be 1000 times as fast.
If you really are iterating over the whole collection, Iterator is obviously still slightly slower but then that's not why you use Iterators: you use them so that you deliberately don't know the access method, so you can change it later without changing your code.
ejpa at 2007-7-12 19:39:09 >

Also, if you happen to switch to LinkedList, iteration with get(i) goes from O(N) to O(N^2).See this thread, starting with reply 10. http://forum.java.sun.com/thread.jspa?threadID=5181709
jverda at 2007-7-12 19:39:09 >
