Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Iterator vs. ListIterator  RSS feed

 
Tyler Peterson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is kind of a general question, but could someone explain to me the difference between an Iterator and a ListIterator, and how they work in a linked list? I'm looking maybe for a description of algorithms, or whatever you think might help me understand.

Thanks!
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go much more eloquently than I could put it.

Iterator -- generic works on all objects needing an iterator or listiterator.
Listiterator--works with lists.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Have you read the documentation for one or the other of those two interfaces? Doesn't that explain the difference?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyler Peterson wrote:and how they work in a linked list?

Exactly the same way as they work in any other List. However, because of the nature of LinkedLists, it's often preferable to use a ListIterator to change them since, once you're at the right position, its add(), remove() and set() methods work in constant time - and are also easier to make Thread-safe.

As an aside, I think ListIterator was badly designed. It's API is overblown, and the business of being "between" elements can make it quite tough to implement.
It also means that you can't implement a single-linked list as a List without involved (and slow) logic to get "previous" elements. I think it would have been much better to simply add a "direction" to the regular Iterator API, along with the add() and set() methods.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!