Java Generics E extends Comparable <E> leaves warning

I'm trying to create 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 the 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 "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;
        }
    }


}

      

+3


source to share


2 answers


The main problem here is that generic <E>

in Node hides 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 it Node

is an inner class, it has direct access to the generic declaration E

declared in LinkedList

. This means that you can easily declare a class Node

without a generic type:

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

      

Then you can easily use variables Node node

inside your class.




Note that if you are declaring Node

as a static class then generation will be required and then you should not be declaring raw variables. It will be:

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

private Node<E> head;

      

Where E

used in static class Node

is different from E

generic declared in LinkedList

.

+7


source


private Node head;

      

This part of the code is throwing a warning. Node expects the type to be declared. Example.

private Node<something> head;

      



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

In your case, you probably want:

private Node<E> head;

      

+2


source







All Articles