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

Recommended Answers

All 6 Replies

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.

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 :-\)

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> ...

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:

private final class CircularIterator implements Iterator<E> {
  // methods go here
}

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:

private final class CircularIterator implements Iterator<E> {
  // methods go here
}

This definately did it. Thank you - my understanding of generic types is still somewhat limited.

> 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. :-)

commented: Good suggestion which is often completely overlooked. +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.