Results 1 to 3 of 3
Thread: Iterator help
- 10-09-2009, 12:51 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
Iterator help
I am trying to apply a hasNext() to an iterator class but having trouble implementing it, here is my code:
Java Code:private class ListIterator<T> implements Iterator<T>{ private LinkedList<T> theList; private Cell<T> current; public ListIterator(LinkedList<T> LL) { theList = LL; current = theList.head; } public boolean hasNext() { Cell<T> p = head; while (n > 0){ if (current==null) return; p = p.next(); n--; } p.current.next; count++; } public T next() { return null; }
The hasNext code I currently in there is not correct.
Can anyone let me know how to approach both of these methods. Any help would be most appreciated.
-
So your iterator holds its own list? I'm no expert in this, but it smells a little funny to me. Why are you doing it this way?
- 10-13-2009, 08:42 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
because i was told to, here is the working code:
Java Code:private class ListIterator<T> implements Iterator<T>{ private LinkedList<T> theList; private Cell<T> current; public ListIterator(LinkedList<T> LL) { theList = LL; current = theList.head; } public boolean hasNext() { return current !=null; } public T next() { T temp = current.data; current = current.next(); return temp; } public void remove() {} }
Similar Threads
-
slow iterator
By jamborta in forum New To JavaReplies: 9Last Post: 05-25-2009, 08:04 AM -
iterator method help..
By mayhewj7 in forum New To JavaReplies: 1Last Post: 04-08-2009, 03:45 AM -
curious about iterator
By jacline in forum New To JavaReplies: 1Last Post: 03-16-2009, 04:29 AM -
iterator
By venkatallu in forum Advanced JavaReplies: 3Last Post: 09-23-2008, 01:32 PM -
Iterator
By eva in forum New To JavaReplies: 0Last Post: 01-31-2008, 02:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks