如何解决如何覆盖双打的compareTo方法?
我目前在理解 compareto 方法如何用于 Comparable 类以及如何覆盖它时遇到了一些麻烦。我有一对数组,每个数组都包含 2 个 double 值,我正在尝试对其进行排序。这是我试过的:
static class Pair implements Comparable<Pair>
{
double x;
double y;
Pair(double x,double y)
{
this.x = x;
this.y = y;
}
public double compareto(Pair other)
{
return y - other.y;
}
}
但是,它不会编译,而是给了我这个错误:
Main.java:5: error: Pair is not abstract and does not override abstract method compareto(Pair) in Comparable
static class Pair implements Comparable<Pair>
^
Main.java:14: error: compareto(Pair) in Pair cannot implement compareto(T) in Comparable
public double compareto(Pair other)
^
return type double is not compatible with int
where T is a type-variable:
T extends Object declared in interface Comparable
2 errors
它适用于整数,但不适用于双精度数,这是为什么呢?我怎样才能让它与双打一起工作?谢谢。
解决方法
compareTo 必须返回一个整数。您可以使用 Double.compare。
public int compareTo(Pair other) {
return Double.compare(y,other.y);
}
,
这可以推广到以下实现,以与 T,V 实现 Comparable
接口的任何 Pair 一起使用。比较不可比较的类的对象通常是没有意义的(在完美的世界中!)。
public class GenComparableStackOverflow {
public static void main(String[] args){
Pair<Double,Double> pair1 = new Pair<>(2.0,2.0);
Pair<Double,Double> pair2 = new Pair<>(4.0,1.0);
Stream.of(pair1,pair2).sorted().forEach(System.out::println); //Uses compareTo
}
}
class Pair<T extends Comparable<T>,V extends Comparable<V>> implements Comparable<Pair<T,V>> {
T x;
V y;
Pair(T x,V y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Pair{" +
"x=" + x +
",y=" + y +
'}';
}
@Override
public int compareTo(Pair<T,V> o) {
return this.y.compareTo(o.y);
}
}
这比您的自定义实现更好,因为 Comparable
的类已经覆盖了 compareTo() 方法。对于您的情况,这将在您不知情的情况下使用 Double.compareTo() :)
例如:
static class Pair implements Comparable<Pair> {
double x;
double y;
Pair(double x,double y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair other) {
if (y > other.y) {
return 1;
} else if(y<other.y) {
return -1;
} else {
return 0;
}
}
}
或(更好的变体):
static class Pair implements Comparable<Pair> {
double x;
double y;
Pair(double x,double y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair other) {
return Double.compare(this.y,other.y);
}
}
,
如果您想要一个通用的 Pair
类,请参阅正确的 Answer by Mohamed Anees A。
如果您想采用专门针对 Pair
原始类型的 double
类的想法,请考虑以下代码。
record
功能
为简洁起见,我使用 Java 16 中新的 record 特性。
作为 record
,构造函数、getter、toString
和 equals
/hashCode
由编译器隐式提供。
Double.compare
对于比较工作,我们只是委托静态 Double.compare
方法来比较两个 double
原语。这种技术出现在 Answer by yuri777 的第二个变体中。
这是完整的 Pair
类。
record Pair(double x,double y) implements Comparable < Pair >
{
@Override
public int compareTo ( Pair o )
{
return Double.compare( this.y,o.y );
}
}
这是一个完整的示例应用程序来演示。
package work.basil.example;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class App2
{
public static void main ( String[] args )
{
App2 app = new App2();
app.demo();
}
private void demo ( )
{
List < Pair > pairs = new ArrayList <>( 3 );
pairs.add( new Pair( 2.1D,3.1D ) );
pairs.add( new Pair( 4D,3D ) );
pairs.add( new Pair( 1.1D,1.1D ) );
System.out.println( "pairs = " + pairs );
Collections.sort( pairs );
System.out.println( "pairs = " + pairs );
}
record Pair(double x,double y) implements Comparable < Pair >
{
@Override
public int compareTo ( Pair o )
{
return Double.compare( this.y,o.y );
}
}
}
运行时:
pairs = [Pair[x=2.1,y=3.1],Pair[x=4.0,y=3.0],Pair[x=1.1,y=1.1]]
pairs = [Pair[x=1.1,y=1.1],Pair[x=2.1,y=3.1]]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。