简体   繁体   中英

Issue creating iterator in java

I'm trying to create an iterator class that completes what I thought would be two simple methods but I am having issues I suppose creating the iterator. The line where I create the iterator is giving me a compile error saying "Iterator is abstract; cannot be instantiated". I am not too sure what that means, obviously I did something wrong though. Also I put the purpose of the methods above them, if you see anything wrong with them let me know. Thanks for any input!

import java.util.Iterator;
private class OrderedListIterator{
  Iterator<E> it = new Iterator<E>();

    //return true if iterator has more items
    public boolean hasNext(){
     boolean found = false;
     if(it.hasNext == true)
        found = true;
        return found;
     return found;    
    }

    //return next item in the iterator  
    public E getNext(){
     if(it.hasNext != false)
        return it.next;
    }

    //prints out message
    public void remove(){
        System.out.println("Operation not supported");
    }
}

The reason you are getting this error is because an iterator is an interface.

In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces. Extension is discussed later in this lesson.

From the Java docs https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html

An interface contains the definition of the methods, not the implementation and is why you can't create or call interfaces or it's methods. The iterator interface has two methods; hasNext() and next(). Your code looks like you intend to implement the iterator interface.

private class OrderedListIterator implements Iterator<E>

In your hasNext and next methods, you need to iterate over your OrderedList depending on how you have implemented it.

Here is an example of an iterator for an ArrayList which I have previously created.

private class ArrayIterator implements Iterator<E> {
    private int arrayIndex = 0;

    /**
     * Checks if the set has a next value.
     * 
     * @return true if there is a next value, else false
     */
    public boolean hasNext() {
        //Checks that the index is within the size of the ArrayList
        return arrayIndex < size;
    }

    /**
     * Gets the next value in the iteration.
     * 
     * @return 
     *      The next value in the list
     * @throws NoSuchElementException
     *      if there is no next element in the list
     */
    public E next() throws NoSuchElementException {
        if (arrayIndex == size) {
            throw new NoSuchElementException();
        }
        //Checks the ArrayList's data at the current index
        return data[arrayIndex++];
    }
}

Your private class is able to access the fields from it's surrounding class. In my example, the iterator stores an index (like an internal cursor) in the array and checks the ArrayList's data at the current index. Each time the next method is called, the index is increased for the next time.

If your OrderedList class is like a LinkedList and has nodes, you would save a reference to the node and each time the next method is called you would return the node, then change the cursor to the next node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM