Results 1 to 8 of 8
- 02-09-2012, 12:13 AM #1
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
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..
- 02-09-2012, 02:36 PM #2
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!
- 02-09-2012, 03:19 PM #3
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.
- 02-09-2012, 08:04 PM #4
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
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.
- 02-09-2012, 10:09 PM #5
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.
- 02-09-2012, 11:27 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Question about LinkedList Iterator
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.I already have a next and previous in my Node class.
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().
- 02-10-2012, 04:18 PM #7
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:
...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.Java Code:List myList = new ArrayList(); public void useList(){ Iterator iter = myList.iterator(); while(iter.hasNext()){ //do something } }
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.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-10-2012, 05:10 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Question about LinkedList Iterator
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Question on .iterator()
By fatabass in forum New To JavaReplies: 7Last Post: 02-08-2012, 12:30 PM -
LinkedList Iterator
By cpguy in forum New To JavaReplies: 8Last Post: 11-17-2011, 05:47 AM -
How to use linkedlist 'add' method without iterator
By plexus0208 in forum New To JavaReplies: 1Last Post: 11-25-2010, 08:27 PM -
Quick LinkedList question
By mac in forum New To JavaReplies: 7Last Post: 05-31-2010, 01:27 AM -
A question about using an iterator
By TaxpayersMoney in forum New To JavaReplies: 8Last Post: 05-21-2010, 08:18 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks