gpt4 book ai didi

java - 如何比较两个泛型类型变量?

转载 作者:行者123 更新时间:2023-12-02 10:56:24 25 4
gpt4 key购买 nike

我的程序:

private class Node{

private d1 val;
private Node next;

public Node(d1 val){
this.val=val;
next=null;
}
}

公共(public)节点排序列表(节点左,节点右){
    try {
Node result=null;
if(left==null) {
return right;
}
if(right==null) {
return left;
}
if(left.val <= right.val) // here erro occurs..
{
result=right;
}
}
catch(NumberFormatException e) {

}

}

最佳答案

您应该实现接口(interface) java.util.Comparator。

public int compare(Node o1, Node o2) {
....
return 0; //(o1 == o2)
....
return 1; //(o1>o2)
....
return -1; //(o1<o2)
}

关于java - 如何比较两个泛型类型变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62749490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com