Win a copy of Modern frontends with htmx this week in the Spring 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79078
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 28145
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79078
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
That is a really big piece of pie for such a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic