Unfortunately, I couldn't find a suitable thread already so please forgive me if there is one already... but!
This was a project due today which I just bit the bullet and left out one part of.
Here is my problem: I am building a circular doubly linked list with a very rigid structure and am getting an incompatible types error when using the inner class to call an outer class method which references a completely separate class.
Thus (this is truncated code - obviously - for simplicity):
public class CircularList<E>
{
/**
* The first node of this list
*/
private CircularNode<E> firstNode;
/**
* Maintains the cursor: current node
*/
public CircularNode<E> cursor;
/**
* The last node of this list
*/
private CircularNode<E> lastNode;
/**
* Many unrelated methods go here
*/
/**
* Returns the circular node at the specified position in this list.
*
* @param index index of a node to return
*
* @returns the node at the specific position in the list
*
* @throws java.lang.IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size())
*/
public CircularNode<E> getNode(int index) throws IndexOutOfBoundsException
{
if (index < 0 || index > size)
throw new IndexOutOfBoundsException();
else
{
CircularNode<E> tempNode = new CircularNode<E>();
tempNode = firstNode;
int tempIndex = 0;
while (tempIndex != index)
{
tempNode = tempNode.getNextNode();
tempIndex++;
}
return tempNode;
}
}
/**
* This class provides an iterator for this CircularList.
*
* @author Bryan Davis
*/
public class CircularListIterator<E> implements java.util.ListIterator<E>{
/**
* The index of nextNode
*/
private int nextIndex;
/**
* The node that this iterator points to
*/
private CircularNode<E> nextNode;
/**
* The last node returned by next() or previous()
*/
private CircularNode<E> previousNode;
/**
* Returns the next element in the list
*/
public E next()
{
nextIndex++;
cursor = cursor.getNextNode();
return cursor.getDataElement();
}
/**
* Returns the previous element in the list
*/
public E previous()
{
nextIndex--;
cursor = cursor.getNextNode();
return cursor.getDataElement();
}
}
}
When I try to compile, I get the following error:
found in CircularList.CircularListIterator<E> Incompatible Types.
Found: E
Required: E
return cursor.getDataElement();
^
for both references.
I know the CircularNode<E> class is properly made, but the method that is referenced is this:
public class CircularNode<E>{
/**
* The data element this node holds
*/
private E dataElement;
/**
* Return the data element of this node
* @return the data element of this node
*/
public E getDataElement(){
return dataElement;
}
}
Again, I'm wondering why I recieve those errors. The instructors provided us with an API which we need to implement a class of, so I am supposed to use the generic type E for both...
Any help would be appreciated.
Edit: I'm sorry, but the tabbing was messed up during copy/paste