Java 无法对基本类型 double 调用 compareTo(double)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20142192/
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-08-12 23:28:24  来源:igfitidea点击:

Cannot invoke compareTo(double) on the primitive type double

javacomparecomparablecompareto

提问by Klausos Klausos

The line return array[index1].compareTo(array[index2]);provides an error "Cannot invoke compareTo(double) on the primitive type double". How to solve this issue?

该行return array[index1].compareTo(array[index2]);提供了一个错误“无法在原始类型 double 上调用 compareTo(double)”。如何解决这个问题?

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::  This function implements a comparator of double values        :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private class ArrayIndexComparator implements Comparator<Integer>
{
    private final double[] array;

    public ArrayIndexComparator(double[] array)
    {
        this.array = array;
    }

    public Integer[] createIndexArray()
    {
        Integer[] indexes = new Integer[array.length];
        for (int i = 0; i < array.length; i++)
        {
            indexes[i] = i; // Autoboxing
        }
        return indexes;
    }

    @Override
    public int compare(Integer index1, Integer index2)
    {
         // Autounbox from Integer to int to use as array indexes
        return array[index1].compareTo(array[index2]);
    }
}


double[] dist = new double[centroids.size()];
// fill array...    
ArrayIndexComparator comparator = new ArrayIndexComparator(dist);
Integer[] indexes = comparator.createIndexArray();
Arrays.sort(indexes, comparator);

采纳答案by dasblinkenlight

Replace the call of an instance method compareTowith the call of static comparemethod, like this:

将实例方法compareTo的调用替换为静态compare方法的调用,如下所示:

return Double.compare(array[index1], array[index2]);

This lets you keep your doubles in an array of primitives, and avoid autoboxing before calling an instance method.

这使您可以将doubles保存在原始数组中,并在调用实例方法之前避免自动装箱。

回答by Prabhakaran Ramaswamy

In java primitive types don't have any methods. Instead using primitive data types use Wrapper classes.

在 Java 原始类型中没有任何方法。使用 Wrapper 类代替使用原始数据类型。

change

改变

return array[index1].compareTo(array[index2]);

to

return  new Double(array[index1]).compareTo(array[index2]);

or

或者

try with Double[] array;instead of double[] array;

尝试 Double[] array;代替 double[] array;

回答by RamonBoza

for primitive types do not use compareTo, use ==instead

对于原始类型不要使用compareTo==而是使用

but if you want to use compareTojust create a Double array

但如果你想使用compareTo只是创建一个 Double 数组

Double[] dist = new Double[centroids.size()];

回答by nimish parikh

Primitive Types cannot be compared directly by a comparator, as the interface is only implemented by collator and RuleBasedCollator. No wrapper class implements comparator. Due to which compiler won't be able to auto box it.

原始类型不能由比较器直接比较,因为接口仅由 collat​​or 和 RuleBasedCollat​​or 实现。没有包装类实现比较器。由于哪个编译器将无法自动装箱。

Just look in Double class and you will find an inbuilt method which provides compare method.

只需查看 Double 类,您就会发现一个提供比较方法的内置方法。

public static int compare(double d1, double d2)

public static int compare(double d1, double d2)

Returns:the value 0 if d1 is numerically equal to d2; a value less than 0 if d1 is numerically less than d2; and a value greater than 0 if d1 is numerically greater than d2.

返回:如果 d1 在数字上等于 d2,则值为 0;如果 d1 在数值上小于 d2,则为小于 0 的值;如果 d1 在数值上大于 d2,则该值大于 0。

Reverse:Multiple the entire expression by -1;

反向:将整个表达式乘以-1;