Dòng return array[index1].compareTo(array[index2]);
cung cấp lỗi "Không thể gọi compareTo (double) trên kiểu nguyên thủy đôi". Giải quyết vấn đề này như thế nào?Không thể gọi compareTo (double) trên kiểu nguyên thủy đôi
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: 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);
sử dụng Double class;) – Alessio
Để khám phá thêm, hãy truy cập tài liệu java và trong đó đi tới gói java.lang.Double, đây được gọi là lớp Wrapper và trong Java cho mọi kiểu nguyên thủy có các lớp trình bao tương ứng. gói java.lang –
Đây là liên kết cho tài liệu http://docs.oracle.com/javase/7/docs/api/ –