Domanda

Im having problems with java generics. When i use next() from the iterator it doesn't return an object of the same type i instantiated it with. So i recieve an incompatible types error. Can anyone help?

I also recieve an Xlint warning when i compile the linked list class.

public class LinkedList<Type>
{

private Node<Type> sentinel = new Node<Type>();
private Node<Type> current;
private int modCount;

public LinkedList()
{
    // initialise instance variables
    sentinel.setNext(sentinel);
    sentinel.setPrev(sentinel);
    modCount = 0;
}
public void prepend(Type newData)
{
   Node<Type> newN = new Node<Type>(newData);
   Node<Type> temp;
   temp = sentinel.getPrev();
   sentinel.setPrev(newN);
   temp.setNext(newN);
   newN.setPrev(temp);
   newN.setNext(sentinel);           
   modCount++;
}


private class ListIterator implements Iterator
{
    private int curPos, expectedCount;
    private Node<Type> itNode;
    private ListIterator()
    {
        curPos =0;
        expectedCount = modCount;
        itNode = sentinel;
    }

    public boolean hasNext()
    {
        return (curPos < expectedCount);
    }

    public Type next()
    {
        if (modCount != expectedCount)
            throw new ConcurrentModificationException("Cannot mutate in context of iterator");
        if (!hasNext())
            throw new NoSuchElementException("There are no more elements");
        itNode = itNode.getNext();
        curPos++;
        current = itNode;
        return (itNode.getData());
    }
 }

}

Here is where the error occurs in the main class after the list is created and filled with different types of shapes.

shape test;
Iterator iter = unsorted.iterator();
test = iter.next();
È stato utile?

Soluzione

Iterator is a generic interface, but your ListIterator is neither generic nor parameterizes Iterator. Start by making ListIterator implement Iterator<Type>:

private class ListIterator implements Iterator<Type> {
    // the rest should be fine
}

or making ListIterator generic as well (more complicated):

private class ListIterator<T> implements Iterator<T>
{
    private int curPos, expectedCount;
    private Node<T> itNode;
    private ListIterator()
    {
        curPos = 0;
        expectedCount = modCount;
        itNode = sentinel;
    }

    public boolean hasNext()
    {
        return (curPos < expectedCount);
    }

    public T next()
    {
        // snip
    }
}

Altri suggerimenti

Could you post code that show how you use it?

Make sure when you use the ListIterator class, you generify it with LinkedList<Something>.ListIterator. Otherwise, an iterator of type LinkedList.ListIterator would be of a raw type and its next() will return Object instead.

Also don't parameterize ListIterator. Otherwise it would be shadowing the type variable on the outer class. Inner (non-static) classes can use outer class's type variables. Also if you did that, you would have to do LinkedList<Something>.ListIterator<Something> to make it consistent; you can't even do LinkedList.ListIterator<Something> because you can't give generic arguments to an inner class of a raw type.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top