Nevets0423
Nevets0423

Reputation: 69

Java Generics E extends Comparable<E> leaves warning

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;
        }
    }


}

Upvotes: 3

Views: 1353

Answers (2)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

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.

Upvotes: 7

brso05
brso05

Reputation: 13232

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;

Upvotes: 2

Related Questions