Results 1 to 3 of 3

Thread: Iterator help

  1. #1
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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?

  3. #3
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    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

  1. slow iterator
    By jamborta in forum New To Java
    Replies: 9
    Last Post: 05-25-2009, 08:04 AM
  2. iterator method help..
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 04-08-2009, 03:45 AM
  3. curious about iterator
    By jacline in forum New To Java
    Replies: 1
    Last Post: 03-16-2009, 04:29 AM
  4. iterator
    By venkatallu in forum Advanced Java
    Replies: 3
    Last Post: 09-23-2008, 01:32 PM
  5. Iterator
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 02:07 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
  •