1

I have been trying to create a linked list that uses generics to return a data type of the user's choosing. The problem is that my method public E get(int sub) is not recognizing my return cursor.contents as a type E generic.

public E get(int sub)
{
    Node cursor = head; //start at the beginning of linked list. 

    for (int c = 1; c <= sub; c++)
    {
        cursor = cursor.next; //move forward by one. 
    }

    return cursor.contents;//return the element that the cursor landed on. 
}


 public class Node <E>
{
        public E contents; 
    @SuppressWarnings("rawtypes")
    public Node next = null; //points to the next node
    //a method has no return type and has the same name as the class
    public Node(E element)
    {
        this.contents = element; 
    }
}

as I have shown above the contents parameter is declared as type E in the Node, but the get method will not recognize cursor.contents as a proper return type.

The system recomments that I either change the return type to Object, which is not an option. Or I change contents to a type E which has already been done, but it still gives me a compilation error.

3
  • Isn't the get method located in the Node class? Commented May 8, 2015 at 4:57
  • I added the generics to the node cursor to make it node<E> cursor, but now when I test it with the code below it gives me a new error and I cant figure out why my code wont return the proper output. Commented May 10, 2015 at 2:13
  • public class Demo1 { public static void main(String[] args) { MyLinkedList<String> t = new MyLinkedList<String>(); t.add("Thousand Oaks"); for (int x = 0; x < t.size(); x++) { System.out.println(t.get(x)); } t.add("Hollywood"); for (int x = 0; x < t.size(); x++) { System.out.println(t.get(x)); } for (int x = 0; x < 500; x++) { t.add("Junk data " + x); } System.out.println(t.get(64)); System.out.println(t.get(499)); System.out.println(t.get(319)); System.out.println("--------"); System.out.println("Cool! I didn't crash!"); } } Commented May 10, 2015 at 2:14

4 Answers 4

1

That's because you need to change it to:

public E get(int sub)
{
    Node<E> cursor = head; //you forgot the generics here

    for (int c = 1; c <= sub; c++)
    {
        cursor = cursor.next; 
    }

    return cursor.contents;
}


 public class Node <E>
{
    public E contents; 
    public Node<E> next = null; //you also even suppressed the raw type here

    public Node(E element)
    {
        this.contents = element; 
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

You should explain where the OP went wrong instead of just providing a copy-pasteable answer
There could be more ;) I think it's interesting to note how and why the original return type disagreed with the declared return type.
1

You're not setting the generic type on the declaration of your Node cursor variable. What happens when you change that to Node<E> cursor.

Also, you're not providing context of the linked list class itself - that's where the generic <E> should be declared.

1 Comment

True, seems like either solution would work! If the first solution is used, however, Node should be a static class. Otherwise the <E> from the LinkedList class can be used instead.
1

In your method

public E get(int sub)

You initialize a cursor as a Node instead of Node<E>.

Node cursor = head; //start at the beginning of linked list. 

This will cause the element type to be Object which is what you get when you write

return cursor.contents;

To Solve:

Either use a Node<E> or explicitly cast the return to E

Comments

0

Are these part of a class with a typed parameter, such as MyLinkedList<E>? The problem may be that you added a <E> type parameter to the Node class as well, which may refer to a different class E that is not necessarily the same E referenced by the outer class. Try changing Node <E> to Node.and see if it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.