Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Question about LinkedList Iterator

  1. #1
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Question about LinkedList Iterator

    For my class I have to re-program a Circular doubly-LinkedList class along with implementing an Iterator. My question is...Is the iterator just so that users can navigate through the Nodes through the main method? I have been reading and the Iterator has data fields that are already in my LinkedList class. For instance, I already have next, and remove..just not hasNext. I just don't understand the importance, or why I would need an Iterator in my Circular doubly-LinkedList..

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Question about LinkedList Iterator

    An Iterator keeps track of where you are in a List, so it can smoothly handle things like remove() without messing up iteration. It's easier to write that logic in an Iterator once rather than rewrite that logic every time you need to look through a List and maybe remove something.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: Question about LinkedList Iterator

    With a circular linked list the Iterator could keep track of where you started and not wrap around when you had looked at all the items in the list. Using an Iterator makes for standard logic when looking through any type of list.

  4. #4
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Question about LinkedList Iterator

    Yes, but I already have a next and previous in my Node class. Then within MyLinkedList class I keep track of the head and tail based on additions and subtractions of Node items. As of right now, everything that I am doing with the iterator can be done in the Node class. So I don't see the actual importance of using an Iterator. I would post my code, but it's about 250 lines long.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: Question about LinkedList Iterator

    Consider this: What if there is a method that will do some work on a list. Its a generic method that works on lots of lists. It requires that the list class implement the Iterator interface to get to its contents.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Question about LinkedList Iterator

    I already have a next and previous in my Node class.
    I'm no expert, but if your next() indicates that there you have reached the end (say by returning null) then it might largely be about style rather than added functionality.

    As KevinWorkman says an iterator provides a handy place to put the rather complex code required to optionally remove elements as you move through a collection.

    Also it might be the case that you want to iterate through a collection, but don't want to do it in the order start-next-next-...-end. Either because there is some reason to deal with particular elements first, or because the collection has different orderings all of which are "natural". Having an iterator (or a number of them) frees you from having to use a class's next().

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Question about LinkedList Iterator

    Also, as Norm hinted at, providing an Iterator allows you to adhere to the List interface. That way people using your LinkedList class could code to the interface rather than the implementation, so they could start out with something that use an ArrayList:

    Java Code:
    List myList = new ArrayList();
    public void useList(){
       Iterator iter = myList.iterator();
       while(iter.hasNext()){
          //do something
       }
    }
    ...now, if the user wanted to change that to your LinkedList, s/he would just have to change a single line. If you didn't provide an iterator, the user would have to change the code everywhere that an iterator was used, and back again if that changed again.

    At a basic level, an iterator might seem extraneous, but once you start dealing with slightly more complicated requirements, you'll see why they come in handy.
    Norm likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Question about LinkedList Iterator

    Quote Originally Posted by KevinWorkman View Post
    Also, as Norm hinted at, providing an Iterator allows you to adhere to the List interface.
    Small nitpick: it's the Iterable interface that makes a List implementation offer the iterator() method. All Collections implement it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Question on .iterator()
    By fatabass in forum New To Java
    Replies: 7
    Last Post: 02-08-2012, 12:30 PM
  2. LinkedList Iterator
    By cpguy in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 05:47 AM
  3. How to use linkedlist 'add' method without iterator
    By plexus0208 in forum New To Java
    Replies: 1
    Last Post: 11-25-2010, 08:27 PM
  4. Quick LinkedList question
    By mac in forum New To Java
    Replies: 7
    Last Post: 05-31-2010, 01:27 AM
  5. A question about using an iterator
    By TaxpayersMoney in forum New To Java
    Replies: 8
    Last Post: 05-21-2010, 08:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •