简体   繁体   English

Java:如何以相反的顺序对浮点数组进行排序?

[英]Java : How to sort an array of floats in reverse order?

I use the following lines to sort an array of floats in reverse order, but I got an error message, what's wrong ? 我使用以下行以相反的顺序对浮点数组进行排序,但是我收到了一条错误消息,出了什么问题?

float sortedData[]=new float[100];
  ...
Arrays.sort(sortedData,Collections.reverseOrder());

Error : cannot find symbol 错误:找不到符号

symbol : method sort(float[],java.util.Comparator) location: class java.util.Arrays Arrays.sort(sortedData,Collections.reverseOrder()); symbol:方法sort(float [],java.util.Comparator)location:class java.util.Arrays Arrays.sort(sortedData,Collections.reverseOrder());

========================================================================= ================================================== =======================

I was confused because in Jdk1.6 api, I saw this : [ Arrays ] public static void sort( float [] a), it doesn't say : public static void sort( Float [] a) 我很困惑,因为在Jdk1.6 api中,我看到了这个:[Arrays] public static void sort( float [] a),它没有说:public static void sort( Float [] a)

That specific method , takes an array of type Object. 该特定方法采用Object类型的数组。 The type float does not extend the Object class, but Float does. float类型不会扩展Object类,但Float会扩展。

Float sortedData[]=new Float[100];
...
Arrays.sort(sortedData,Collections.reverseOrder());

我建议使用Arrays.sort(float[])然后编写一个reverse(float[])方法reverse(float[]) (你可能想也可能不想转移NaNs)。

there is no Arrays.sort(float[], Comparator) method; 没有Arrays.sort(float[], Comparator)方法; however you can use or Arrays.asList() or just use a boxed Float[] array: 但是你可以使用或Arrays.asList()或只使用盒装的Float []数组:

Float[] sortedData = new Float[100];
...
Arrays.sort(sortedData, Collections.reverseOrder());

In order to box a primitive array you can use the following code: 为了封装基本数组,您可以使用以下代码:

public static Float[] floatArray(float... components) {
    return toBoxedArray(Float.class, components);
}

private static <T> T[] toBoxedArray(Class<T> boxClass, Object components) {
    final int length = Array.getLength(components);
    Object res = Array.newInstance(boxClass, length);

    for (int i = 0; i < length; i++) {
        Array.set(res, i, Array.get(components, i));
    }

    return (T[]) res;
}

or include something like commons lang in your project and use ArrayUtils 或者在项目中包含像commons lang这样的东西并使用ArrayUtils

The compiler isn't lying to you. 编译器不是骗你的。 There isn't a method in Arrays called "sort" that takes an array of float and a Comparator. Arrays中没有一个名为“sort”的方法,它接受一个float数组和一个Comparator。 There is, however, a method called "sort" which takes an array of Objects and a Comparator. 但是,有一种称为“排序”的方法,它采用对象和比较器的数组。 Perhaps if you converted your array to an array of Float before you called sort? 也许如果在调用sort之前将数组转换为Float数组?

Think of it as a defect in Java's autoboxing if you will, that it can't autobox an array of primitives into an array of the equivalent Objects. 可以把它想象成Java自动装箱中的缺陷,如果你愿意的话,它不能将基元数组自动放入等效对象的数组中。

Guava has a method Floats.asList() for creating a List<Float> backed by a float[] array. Guava有一个Floats.asList()方法,用于创建一个由float[]数组支持的List<Float> You can use this with Collections.sort to apply the Comparator to the underlying array. 您可以将它与Collections.sort一起使用,以将Comparator应用于基础数组。

List<Float> floatList = Floats.asList(arr);
Collections.sort(floatList, Collections.reverseOrder());

Note that the list is a live view backed by the actual array, so it should be pretty efficient. 请注意,该列表是由实际数组支持的实时视图,因此它应该非常高效。

Others have explained why. 其他人解释了原因。 Can you sort it first and then reverse it? 你可以先排序然后反转吗?

The same, using autoboxing in the for-loop: 同样,在for循环中使用自动装箱:

double[] dbArray = {2.0, 3.0, 4.0, 5.0};
Double[] dBArray = new Double[dbArray.length];
int i = 0;

for(Double db : dbArray){
    dBArray[i] = db; i++;
}
Arrays.sort(dBArray, Collections.reverseOrder());

With Java 8, you can make use of Steam APIs like below - 使用Java 8,您可以使用如下的Steam API -

(There is no FloatStream and direct mapToFloat method but you can always use double) (没有FloatStream和直接mapToFloat方法,但你总是可以使用double)

float[] unsorted = new float[100];
....
List<Double> sorted = IntStream.range(0, unsorted.length).mapToDouble(i->unsorted[i]).boxed().sorted(Collections.reverseOrder()).collect(Collectors.toList());
  1. Code using boxed method to converting primitive to object type. 使用盒装方法将原语转换为对象类型的代码。
  2. Collections.reverseOrder() aids to the sorting in reverse order. Collections.reverseOrder()以相反的顺序帮助排序。
  3. At last collecting it to list, here you can use other types as well. 最后收集它到列表,在这里你也可以使用其他类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM