Ok this is my first time doing iterators in java so bear with.

Here is what my assignment is asking for:

You should add the following methods to the code provided by the authors:
public ListIterator <E> iterator()

The type of iterator is actually ListIterator because this can be easily implemented because there is already a list within the class – just use its listIterator method.

public int size()
Getting the size of the list (you already have) is easy

public void remove(E obj)
Lists always already have a remove method, just use it.

public OrderedList<E> getSubLIst(E obj1, E obj2)

This produces and returns an OrderedList (the same type as the class that contains it). Since the list is already ordered, it is easy to create another ordered list. Examine the elements of the existing list comparing them against obj1 and obj2. The element is placed in the new list if it is greater than or equal to obj1 and less than obj2.

Here is what I have come up with so far, I think I'm on the right track for doing the comparing of objects and then adding the element into a new list, I don't know if I'm close on the other parts of the assignment though.:

public ListIterator <E> iterator(){
		return theList.iterator();
	}

	public void remove(E obj){

	}
	public int size(){
		return theList.size();
	}
	public OrderedList<E> getSubList(E obj1, E obj2){
		if (obj.compareTo(theList.next()) < obj2) {
			iter.previous();
			iter.add(obj);
			return;
		if (obj.compareTo(theList.next()) > obj1) {
			iter.previous();
			iter.add(obj);
			return;
			}
		}
		iter.add(obj);
	}

Here is the code that I'm suppose to be adding too:

public class OrderedList<E extends Comparable<E>>
        implements Iterable<E> {
    /** A linked list to contain the data. */
    private LinkedList<E> theList = new LinkedList<E>();

  public OrderedList() {
    theList = new LinkedList < E > ();
  }

  /** Insert obj into the list preserving the lists order.
      pre: The items in the list are ordered.
      post: obj has been inserted into the list
      such that the items are still in order.
      @param obj The item to be inserted
   */
  public void add(E obj) {
    ListIterator < E > iter = theList.listIterator();
    // Find the insertion position and insert.
    while (iter.hasNext()) {
      if (obj.compareTo(iter.next()) < 0) {
        // Iterator has stepped over the first element
        // that is greater than the element to be inserted.
        // Move the iterator back one.
        iter.previous();
        // Insert the element.
        iter.add(obj);
        // Exit the loop and return.
        return;
      }
    }
    // assert: All items were examined and no item is larger than
    // the element to be inserted.
    // Add the new item to the end of the list.
    iter.add(obj);
  }

  /** Returns the element at the specified position.
      @param index The index of the specified position
      @return The element at position index
   */
  E get(int index) {
    return theList.get(index);
  }

Thanks for your help.

Ok I have made some changes, mainly to reflect the testfile that has been given out.

Am I getting on the right track so far for the code that I have to add to the orignal that is in the previous post?

public ListIterator <E> iterator(){
		return myList.iterator();
	}
	public int size(){
		iteratormyList.size();
	}
	public void remove(E obj){
		iteratormyList.remove();
	}
	public OrderedList<E> getSubList(E obj1, E obj2){
			if (obj.compareTo(myList.next()) < obj2) && (obj.compareTo(myList.next()) >= obj1) {			
				iter.previous();			
				iter.add(obj);			
				return;		
			}		
		iter.add(obj);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.