0

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();
2
  • Are you sure that you're not using raw types (e.g. plain LinkedList instead of LinkedList<String>) anywhere in your code? That would be a possible explanation of what you're describing. Please show the code where you're actually calling next, where the error is happening. Commented May 6, 2013 at 3:13
  • private Node current; should be private Node<Type> current;? Commented May 6, 2013 at 3:18

2 Answers 2

2

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
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm still having the same problem after changing the iterator.
Aside from the iterator not implementing remove() the basic code compiles just fine for me. ideone.com/OTYxvP
if he does the latter, he should also make ListIterator not an inner class
0

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.

1 Comment

Ah Thakyou both guys! it needed to be Iterator<shape> iter = unsorted.iterator(); instead of Iterator iter = unsorted.iterator();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.