944,121 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1179
  • Java RSS
Oct 21st, 2009
0

Incompatible types with generics and an inner class.

Expand Post »
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):

Java Syntax (Toggle Plain Text)
  1. public class CircularList<E>
  2. {
  3. /**
  4.  
  5.   * The first node of this list
  6.  
  7.   */
  8.  
  9. private CircularNode<E> firstNode;
  10.  
  11. /**
  12.  
  13. * Maintains the cursor: current node
  14.  
  15. */
  16.  
  17. public CircularNode<E> cursor;
  18.  
  19. /**
  20.  
  21.   * The last node of this list
  22.  
  23.   */
  24.  
  25. private CircularNode<E> lastNode;
  26.  
  27. /**
  28. * Many unrelated methods go here
  29. */
  30.  
  31. /**
  32.  
  33.   * Returns the circular node at the specified position in this list.
  34.  
  35.   *
  36.  
  37.   * @param index index of a node to return
  38.  
  39.   *
  40.  
  41.   * @returns the node at the specific position in the list
  42.  
  43.   *
  44.  
  45.   * @throws java.lang.IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size())
  46.  
  47.   */
  48.  
  49. public CircularNode<E> getNode(int index) throws IndexOutOfBoundsException
  50.  
  51. {
  52. if (index < 0 || index > size)
  53.  
  54. throw new IndexOutOfBoundsException();
  55.  
  56. else
  57.  
  58. {
  59.  
  60. CircularNode<E> tempNode = new CircularNode<E>();
  61.  
  62.  
  63.  
  64. tempNode = firstNode;
  65.  
  66.  
  67.  
  68. int tempIndex = 0;
  69.  
  70.  
  71.  
  72. while (tempIndex != index)
  73.  
  74. {
  75.  
  76. tempNode = tempNode.getNextNode();
  77.  
  78.  
  79.  
  80. tempIndex++;
  81.  
  82. }
  83.  
  84.  
  85.  
  86. return tempNode;
  87.  
  88. }
  89.  
  90. }
  91.  
  92.  
  93. /**
  94.  
  95.   * This class provides an iterator for this CircularList.
  96.  
  97.   *
  98.  
  99.   * @author Bryan Davis
  100.  
  101.   */
  102.  
  103. public class CircularListIterator<E> implements java.util.ListIterator<E>{
  104.  
  105. /**
  106.  
  107.   * The index of nextNode
  108.  
  109.   */
  110.  
  111. private int nextIndex;
  112.  
  113.  
  114.  
  115. /**
  116.  
  117.   * The node that this iterator points to
  118.  
  119.   */
  120.  
  121. private CircularNode<E> nextNode;
  122.  
  123.  
  124.  
  125. /**
  126.  
  127.   * The last node returned by next() or previous()
  128.  
  129.   */
  130.  
  131. private CircularNode<E> previousNode;
  132.  
  133.  
  134.  
  135. /**
  136.  
  137.   * Returns the next element in the list
  138.  
  139.   */
  140.  
  141. public E next()
  142.  
  143. {
  144.  
  145. nextIndex++;
  146.  
  147. cursor = cursor.getNextNode();
  148.  
  149. return cursor.getDataElement();
  150.  
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157. /**
  158.  
  159.   * Returns the previous element in the list
  160.  
  161.   */
  162.  
  163. public E previous()
  164. {
  165.  
  166. nextIndex--;
  167.  
  168. cursor = cursor.getNextNode();
  169.  
  170. return cursor.getDataElement();
  171.  
  172. }
  173.  
  174. }
  175. }

When I try to compile, I get the following error:

Java Syntax (Toggle Plain Text)
  1. found in CircularList.CircularListIterator<E> Incompatible Types.
  2. Found: E
  3. Required: E
  4. return cursor.getDataElement();
  5. ^

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)
  1. public class CircularNode<E>{
  2.  
  3. /**
  4.  
  5.   * The data element this node holds
  6.  
  7.   */
  8.  
  9. private E dataElement;
  10.  
  11. /**
  12.  
  13.   * Return the data element of this node
  14.  
  15.   * @return the data element of this node
  16.  
  17.   */
  18.  
  19. public E getDataElement(){
  20.  
  21. return dataElement;
  22.  
  23. }
  24. }


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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Fire00f1y is offline Offline
8 posts
since May 2009
Oct 21st, 2009
0
Re: Incompatible types with generics and an inner class.
If you're done like you say, then post all of your code including the main so that I can run it. I'll see if I can fix it which I probably can, but generics are a little rusty in my mind right now and I see nothing right away.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 21st, 2009
0
Re: Incompatible types with generics and an inner class.
Okay, let me get back to lab where we keep the code. I'll have the whole thing posted by tonight (I simply did not implement an iterator in the project :-\)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Fire00f1y is offline Offline
8 posts
since May 2009
Oct 21st, 2009
0
Re: Incompatible types with generics and an inner class.
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> ...
Featured Poster
Reputation Points: 1938
Solved Threads: 953
Posting Expert
JamesCherrill is offline Offline
5,809 posts
since Apr 2008
Oct 21st, 2009
0
Re: Incompatible types with generics and an inner class.
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> ...
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)
  1. private final class CircularIterator implements Iterator<E> {
  2. // methods go here
  3. }
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 21st, 2009
0
Re: Incompatible types with generics and an inner class.
Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
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)
  1. private final class CircularIterator implements Iterator<E> {
  2. // methods go here
  3. }
This definately did it. Thank you - my understanding of generic types is still somewhat limited.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Fire00f1y is offline Offline
8 posts
since May 2009
Oct 21st, 2009
1
Re: Incompatible types with generics and an inner class.
> 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. :-)
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how to call and search an array of one class in other class ?
Next Thread in Java Forum Timeline: Algorithm that runs in O(n)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC