【问题标题】:Java Generics E extends Comparable<E> leaves warningJava 泛型 E 扩展 Comparable<E> 留下警告
【发布时间】:2014-12-08 12:28:17
【问题描述】:

我正在尝试创建一个通用类,E extends Comparable E 但我在 Eclipse 中收到一条警告:

LinkedList.Node 是一个原始类型。对泛型 LinkedList E .Node E 的引用应该被参数化

代码如下:

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


}

【问题讨论】:

标签: java generics


【解决方案1】:

这里的主要问题是 Node 中的通用 &lt;E&gt; 隐藏了 LinkedList&lt;E extends Comparable&lt;E&gt;&gt; 中的 E。此警告应出现在此处:

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
}

由于Node 是一个内部类,它可以直接访问LinkedList 中声明的泛型E。这意味着,您可以轻松地声明 Node 类而无需泛型类型:

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

然后您可以轻松地在您的类中使用Node node 变量。


请注意,如果您将Node 声明为静态类,那么泛型将是必需的,那么您不应声明原始变量。这将是:

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

private Node<E> head;

static class Node 中使用的E 与LinkedList 中声明的E 泛型不同。

【讨论】:

    【解决方案2】:
    private Node head;
    

    这段代码抛出了警告。 Node 在声明时期望有一个类型。例如。

    private Node<something> head;
    

    你没有指定任何东西,所以它警告你你没有指定类型。

    在你的情况下你可能想要:

    private Node<E> head;
    

    【讨论】:

    • 叹息我只是以正确的方式使用反对票:这个答案没有用。如果您认为我的回答没有用,请投票给我,我不会生气:)。你太认真了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 2020-12-28
    • 2015-06-17
    相关资源
    最近更新 更多