| | |
Incompatible types with generics and an inner class.
Thread Solved |
•
•
Join Date: May 2009
Posts: 8
Reputation:
Solved Threads: 0
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):
When I try to compile, I get the following error:
for both references.
I know the CircularNode<E> class is properly made, but the method that is referenced is this:
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
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):
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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
0
#5 Oct 21st, 2009
•
•
•
•
Maybe the use of <E> in the iterator class is taken as a new declaration of a type, and masks the <E> that is declared for the outer class? If so, maybe you could declare the iterator class as
public class CircularListIterator<F extends E> ...
Java Syntax (Toggle Plain Text)
private final class CircularIterator implements Iterator<E> { // methods go here }
I don't accept change; I don't deserve to live.
•
•
Join Date: May 2009
Posts: 8
Reputation:
Solved Threads: 0
0
#6 Oct 21st, 2009
•
•
•
•
IMO, though the analysis is correct, the solution provided isn't because an Iterator over a collection of elements of type E isn't the same as an Iterator over a collection of elements of type F which extends E. The solution here is to declare CircularListIterator like:Java Syntax (Toggle Plain Text)
private final class CircularIterator implements Iterator<E> { // methods go here }
1
#7 Oct 21st, 2009
> Thank you - my understanding of generic types is still somewhat limited.
When in doubt, always look at the standard library code which comes prepackaged with the JDK. For e.g. in this case, looking at the source of the ArrayList.java file would have solved your problem. :-)
When in doubt, always look at the standard library code which comes prepackaged with the JDK. For e.g. in this case, looking at the source of the ArrayList.java file would have solved your problem. :-)
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Incompatible types (Java)
- java help needed - incompatible types (Java)
- Unexpected Type/Incompatible Types (Java)
- Incompatible Types? (Java)
- error: incompatible types in assignment (C)
- "Incompatible Types" Error In an IP Routing Program (Java)
Other Threads in the Java Forum
- Previous Thread: how to call and search an array of one class in other class ?
- Next Thread: Algorithm that runs in O(n)
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






