Cannot invoke compareTo(double) on the primitive type double
Asked Answered
R

4

13

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?

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::  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);
Repeater answered 22/11, 2013 at 10:9 Comment(2)
To explore more visit the java documentation and within that go to the package java.lang.Double, this is called a Wrapper class and in Java for every primitive type there are corresponding wrapper classes are present under the java.lang packageDukas
Here is the link for documentation docs.oracle.com/javase/7/docs/apiDukas
C
25

Replace the call of an instance method compareTo with the call of static compare method, like this:

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.

Couscous answered 22/11, 2013 at 10:13 Comment(2)
How to sort in reverse order in this case? Where to put ,Collections.reverseOrder()?Repeater
@KlausosKlausos Collections.reverseOrder() is for reversing the natural order. Since you are using a comparator, you can switch the order inside it by calling Double.compare(array[index2], array[index1]), or by adding an extra boolean isReverseOrder flag to the constructor, storing it in a member variable, and inverting the comparison result inside the compare method when isReverseOrder is set.Couscous
O
1

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

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;

Oehsen answered 22/11, 2013 at 10:11 Comment(0)
J
0

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

but if you want to use compareTo just create a Double array

Double[] dist = new Double[centroids.size()];
Jaborandi answered 22/11, 2013 at 10:14 Comment(1)
== is not an equivalent of compareTo but of equals.Salute
C
0

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.

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

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.

Reverse: Multiple the entire expression by -1;

Coppice answered 6/3, 2017 at 3:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.