GeeCON Prague 2014*
The moose likes Java in General and the fly likes we can also retrieve elements of an arraylist using a loop instead of iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "we can also retrieve elements of an arraylist using a loop instead of iterator" Watch "we can also retrieve elements of an arraylist using a loop instead of iterator" New topic
Author

we can also retrieve elements of an arraylist using a loop instead of iterator

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
we can also retrieve elements of an arraylist using a loop instead of iterator.whats the difference then? whats wrong in fetching arralist elements using loop?

thanks..
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
Let me confirm that this is what you mean:

Loop:



Iterator:



If this is indeed what you're talking about, then there isn't much difference.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Emanuel Kadziela wrote: . . .
Loop:



Iterator:



If this is indeed what you're talking about, then there isn't much difference.
There is hardly any difference. The first form, the for‑each (enhanced for) loop is syntactic sugar for something like this (I think I have got this bit right):-So both use loops. We already know that the two conventional ways to iterate a collection are
Iterator<XXX> it = coll.iterator; while(it.hasNext())...
and
for (Iterator<XXX> it = coll.iterator; it.hasNext(); )...
So what you have seen are simply the two conventional ways to use an Iterator, one of them being “hidden” behind the for‑each loop.

What you have forgotten is hinted at here. You have a tagging interface which suggests you can use this form of for loop:-You will have seen the similarity to a conventional for loop to iterate an array. This only works well if the list is backed by an array. But we know that access to element 123456789 of an array is as fast as access to element 1. That is what “random” access means. So you see the javadoc link I gave you contradicts the conventional advice always to use an Iterator. It tells you all about it. It works very badly for linked lists, and very well for lists backed by an array, so you can combine the two:-I think that is what you meant about two kinds of loop.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2297
    
  49
If this is indeed what you're talking about, then there isn't much difference.

Both these code snippets use the list's Iterator so I'm not sure if it is what the OP meant. I suspect they were talking about the difference between an Iterator and a standard for loop.
You are correct in that there isn't much difference but there is a small important difference between the two and that is by directly using an iterator you can use it to safely remove the current loop object from the list and still continue to iterate over the list. You can't do this when using a for-each loop.

The for loop implementation is different in that you use the get() method to directly retrieve the object at the given index. This means you can do things like get adjacent objects for comparison; get every nth object; get objects in reverse order etc etc.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Tony Docherty wrote:The for loop implementation is different in that you use the get() method to directly retrieve the object at the given index. This means you can do things like get adjacent objects for comparison; get every nth object; get objects in reverse order etc etc.


And in this case, it makes a performance difference if you use a LinkedList versus an ArrayList when you use get(n) to retrieve the object at index n. (I believe it's O(N) for the ArrayList versus O(N^2) for the LinkedList.) Whereas using the Iterator for either kind of list provides O(N) performance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
For an arrayed list, which usually implements the RandomAccess interface, finding the object at position i runs in constant time, so iterating the whole list like that is linear time. It would usually be linear time to find an element in for a linked list, but iterating the whole linked list is quadratic time (I did say yesterday, “It works very badly for linked lists”).
The advantage of using the Iterator on a linked list is that going from item i to item i + 1 is constant time, whereas finding item i with get(i) is much slower.
Another advantage of get() on an arrayed list is that you can iterate part of the list, maybe starting in the middle.
 
GeeCON Prague 2014
 
subject: we can also retrieve elements of an arraylist using a loop instead of iterator