Forums Register Login

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

+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4108 times.
Similar Threads
trouble inhasNext
Best practice for comparing values in a JSP
Android Training
Give the easiest example which differentiate "interface" and "abstract class" in Java.
int or Integer
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:25:52.