Incompatible types with generics and an inner class.

Thread Solved

Join Date: May 2009
Posts: 8
Reputation: Fire00f1y is an unknown quantity at this point 
Solved Threads: 0
Fire00f1y Fire00f1y is offline Offline
Newbie Poster

Incompatible types with generics and an inner class.

 
0
  #1
Oct 21st, 2009
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):

  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:

  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:

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,566
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #2
Oct 21st, 2009
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.
Out.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 8
Reputation: Fire00f1y is an unknown quantity at this point 
Solved Threads: 0
Fire00f1y Fire00f1y is offline Offline
Newbie Poster
 
0
  #3
Oct 21st, 2009
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 :-\)
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark
 
0
  #4
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> ...
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human
 
0
  #5
Oct 21st, 2009
Originally Posted by JamesCherrill View Post
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:
  1. private final class CircularIterator implements Iterator<E> {
  2. // methods go here
  3. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 8
Reputation: Fire00f1y is an unknown quantity at this point 
Solved Threads: 0
Fire00f1y Fire00f1y is offline Offline
Newbie Poster
 
0
  #6
Oct 21st, 2009
Originally Posted by ~s.o.s~ View Post
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human
 
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. :-)
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC