Results 1 to 3 of 3
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default [SOLVED] Writing an Iterator method in a custom list

    I have a DoublyLinkedList class that uses a circular-doubly-linked list to store values of a LongInteger and I need to implement an interator method within my DoublyLinkedList class that returns an iterator. The thing is the book tells us to do
    Java Code:
    public Iterator<T> iterator() {
    	return new LinkedIterator<T>(first,size);
    }
    however LinkedIterator isn't actually in Java and that is the only time it's mentioned in my book. I know essentially that my iterator() method is supposed to return an iterator but I can't figure out how to write one. Any helpful links or code?
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I've updated it but now it's stopping one short.
    Java Code:
    public Iterator iterator() {
    	return new LinkedIterator(this);
    }
    my LinkedIterator class looks like this
    Java Code:
    public class LinkedIterator<T> implements Iterator<T> {
    
    	private int count;
    	private DoubleNode<T> current;
    	private DoubleNode<T> last;
    	private DoublyLinkedList items;
    	
    	public LinkedIterator(DoublyLinkedList doublyLinkedList){
    		items = doublyLinkedList;
    		last = items.last();
    		current = last;
    	}
    	public boolean hasNext() {
    		if (current.getNext() != last){
    			current = current.getNext();
    			return true;
    		}else{
    			return false;
    		}
    	}
    	public T next() {
    		return current.getData();
    	}
    	public void remove() {		
    	}
    }
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    changed my has next to go off items.size instead. It works now.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

Similar Threads

  1. Replies: 9
    Last Post: 11-04-2011, 03:09 AM
  2. Revised Linked List printing method question
    By CirKuT in forum New To Java
    Replies: 7
    Last Post: 12-12-2008, 10:21 PM
  3. question about writing a toString method
    By heather.diggs in forum New To Java
    Replies: 2
    Last Post: 11-12-2008, 11:13 PM
  4. Creating an Iterator for Linked List
    By spanky in forum New To Java
    Replies: 3
    Last Post: 10-05-2008, 07:16 PM
  5. Writing the filter method
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2007, 03:32 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
  •