」無法調用compareTo(double)return array[index1].compareTo(array[index2]);
行提供了一個錯誤「」無法在基本類型雙「」上調用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);
使用雙類;) – Alessio
要了解更多信息,請訪問java文檔,並在其中轉到包java.lang.Double,這稱爲Wrapper類,並且在Java中對於每種基本類型都有對應的包裝類java.lang包 –
以下是文檔鏈接http://docs.oracle.com/javase/7/docs/api/ –