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..
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.
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.
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.
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.
Re: Question about LinkedList Iterator
Quote:
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().
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:
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.
Re: Question about LinkedList Iterator
Quote:
Originally Posted by
KevinWorkman
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