3

I'm trying to make a Generic class where E extends Comparable E but I get a warning in Eclipse that says:

LinkedList.Node is a raw type. References to generic type LinkedList E .Node E should be parameterized

Here is the code:

public class LinkedList<E extends Comparable<E>>



{
    // reference to the head node.
    private Node head;
    private int listCount;

    // LinkedList constructor
 public void add(E data)
    // post: appends the specified element to the end of this list.
    {
        Node temp = new Node(data);
        Node current = head;
        // starting at the head node, crawl to the end of the list
        while(current.getNext() != null)
        {
            current = current.getNext();
        }
        // the last node's "next" reference set to our new node
        current.setNext(temp);
        listCount++;// increment the number of elements variable
    }
 private class Node<E extends Comparable<E>>
    {
        // reference to the next node in the chain,
        Node next;
        // data carried by this node.
        // could be of any type you need.
        E data;


        // Node constructor
        public Node(E _data)
        {
            next = null;
            data = _data;
        }

        // another Node constructor if we want to
        // specify the node to point to.
        public Node(E _data, Node _next)
        {
            next = _next;
            data = _data;
        }

        // these methods should be self-explanatory
        public E getData()
        {
            return data;
        }

        public void setData(E _data)
        {
            data = _data;
        }

        public Node getNext()
        {
            return next;
        }

        public void setNext(Node _next)
        {
            next = _next;
        }
    }


}
Justin
  • 1,972
  • 13
  • 28
Nevets0423
  • 69
  • 5
  • `Node` should be parametrized (with E) when used like here: `private Node head;` –  Oct 13 '14 at 20:40
  • 1
    @LuiggiMendoza Alright :( [Please read this to understand raw types.](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Sotirios Delimanolis Oct 13 '14 at 20:55

2 Answers2

7

The main problem here is that generic <E> in Node hides the E from LinkedList<E extends Comparable<E>>. This warning should appear here:

private class Node<E extends Comparable<E>> {
                   ^ here you should get a warning with the message
                   The type parameter E is hiding the type E
}

Since Node is an inner class, it has direct access to the generic E declared in LinkedList. This means, you can easily declare Node class without the generic type:

private class Node {
    E data;
    Node next;
    //rest of code...
}

Then you can easily use Node node variables inside your class.


Note that if you declare Node as a static class, then the generic would be necessary and then you should not declare raw variables. This would be:

private static Node<E extends Comparable<E>> {
    E data;
    Node<E> next;
    //rest of code...
}

private Node<E> head;

Where the E used in static class Node is different from the E generic declared in LinkedList.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
2
private Node head;

This piece of code is throwing the warning. Node expects to have a type when it is declared. Ex.

private Node<something> head;

You are not specifying anything so it is warning you that you have not specified a type.

In your case you probably want:

private Node<E> head;
brso05
  • 13,142
  • 2
  • 21
  • 40
  • *Sigh* I just use the downvote in its right way: *This answer is not useful*. If you think my answer is not useful, just downvote me, I won't get mad :). You're taking this too seriously. – Luiggi Mendoza Oct 13 '14 at 21:16