java 如何使用 Comparable 比较链表中的通用节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6356220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:32:07  来源:igfitidea点击:

How to compare generic nodes in a linked list using Comparable?

javagenericspriority-queuesortedlist

提问by hash

I am implementing a sorted list using linked lists. My node class looks like this

我正在使用链接列表实现排序列表。我的节点类看起来像这样

public class Node<E>{
    E elem;
    Node<E> next, previous;
}

In the sorted list class I have the add method, where I need to compare generic objects based on their implementation of compareTo() methods, but I get this syntax error "The method compareTo(E) is undefined for type E". I have tried implemnting the compareTo method in Node, but then I can't call any of object's methods, because E is generic type. Here is the non-finished body of add(E elem) method.

在排序列表类中,我有 add 方法,我需要根据它们对 compareTo() 方法的实现来比较通用对象,但我收到此语法错误“方法 compareTo(E) 未定义为类型 E”。我曾尝试在 Node 中实现 compareTo 方法,但后来我无法调用任何对象的方法,因为 E 是泛型类型。这是 add(E elem) 方法的未完成主体。

public void add(E elem) 
{

        Node<E> temp = new Node<E>();
        temp.elem = elem;

        if( isEmpty() ) {           
            temp.next = head;
            head.previous = temp;
            head = temp;
            counter++; 
        }else{
            for(Node<E> cur = head; cur.next != null ; cur= cur.next) {
                **if(temp.elem.comparTo(cur.elem)) {**
                    //do the sort;

                }/*else{
                    cur.previous = temp;
                }*/             
            }
            //else insert at the end

        }
}

Here is one of the object implemnting compareTo method

这是实现 compareTo 方法的对象之一

public class Patient implements Comparable<Patient>{
    public int compareTo(Patient that)
    {
        return (this.getPriority() <= that.getPriority() ? 1 : 0 );
    }
}

采纳答案by Bohemian

Bound E to Comparable:

将 E 绑定到 Comparable:

public class Node<E extends Comparable<E>>{
    E elem;
    Node<E> next, previous;
}

It will compile now.

它现在会编译。

回答by Sorrow

It seems that your generic Emust be E extends Comparable<E>. This way you will get the access to the compareTo(E other)method. However, you will be unable to add elements that are not implementing this interface.

看来您的泛型E必须是E extends Comparable<E>. 这样您就可以访问该compareTo(E other)方法。但是,您将无法添加未实现此接口的元素。

回答by Waldheinz

If you want the elements stored in your nodes to be comparable, you can state this using generics:

如果您希望存储在节点中的元素具有可比性,您可以使用泛型来说明这一点:

public class Node<E extends Comparable<E>> {

    E elem;
    Node<E> next, previous;
}

this way it is sure, that every Eimplements the Comparableinterface, so you can safely call the compareTomethod.

这样可以确定,每个都E实现了Comparable接口,因此您可以安全地调用该compareTo方法。

回答by Dorus

Try

尝试

public class Node<E extends Comparable<E>>{
    E elem;
    Node<E> next, previous;
}