Java 从原始数组获取已排序数组的索引位置

Java 从原始数组获取已排序数组的索引位置,java,arrays,Java,Arrays,我有一个数组(distCent),它包含几个双值 double[] distCent = {0.34, 0.12, 0.01, 0.45, 0.65, 0.78}; 我想得到数组中前5个最小值的索引位置(x)。我的期望输出将如下所示: Smallest value is at position 3 with the value of 0.01 2nd smallest value is at position 2 with the value of 0.12 3rd smallest valu

我有一个数组(distCent),它包含几个双值

double[] distCent = {0.34, 0.12, 0.01, 0.45, 0.65, 0.78};
我想得到数组中前5个最小值的索引位置(x)。我的期望输出将如下所示:

Smallest value is at position 3 with the value of 0.01
2nd smallest value is at position 2 with the value of 0.12
3rd smallest value is at position x with the value of y
4th smallest value is at position x with the value of y
5th smallest value is at position x with the value of y
为了实现这一点,我将数组按从低到高的顺序进行排序,如下所示:

Arrays.sort(distCent);//use sort 
  System.out.println(Arrays.asList(distCent)); //the value in the array will be sorted
现在,我不确定我如何才能获得前5名的指数位置,从而产生我预期的产出,或者以其他更好的方式实现这一目标?有人能帮忙吗?谢谢

import java.util.ArrayList;
import java.util.ArrayList;
...
ArrayList<double> temp = new ArrayList<double>();
for(double i : distCent){
    temp.add(i);
}
Arrays.sort(distCent);
for(int x = 0; x < 5; x++){
    //index is the original location
    int index = temp.indexOf(distCent[x]);
}
... ArrayList temp=新的ArrayList(); 用于(双i:膨胀){ 临时添加(i); } 数组。排序(距离); 对于(int x=0;x<5;x++){ //索引是原始位置 int index=温度指数(膨胀[x]); }

要保留原始索引,需要创建原始数组的副本,然后对照排序数组检查值。Java中的列表有一个方便的indexOf方法来实现这一点。

使用对象将数据值与索引配对

定义配对类,例如:

public class Pair implements Comparable<Pair> {
    double value;
    int index;

    public Pair(double v, int i) {
        value = v;
        index = i;
    }
    public int compareTo(Pair p) {
        if (value - p.value < 0) return -1;
        if (value - p.value > 0) return 1;
        return 0;
}
现在,在排序之后,当您访问数组时,您可以看到索引

distCent[i].索引//这是项目的原始索引

我建议使用自定义打印方法而不是asList方法,因为它提供了更大的灵活性。您可以使用自定义方法打印索引。

试试这个

double[] distCent = {0.34, 0.12, 0.01, 0.45, 0.65, 0.78};
String[] names = {"Smallest", "2nd smallest", "3rd smallest", "4th smallest", "5th smallest"};
int[] c = {0};
IntStream.range(0, distCent.length)
    .mapToObj(n -> new double[]{n, distCent[n]})
    .sorted(Comparator.comparing(a -> a[1]))
    .limit(names.length)
    .forEach(a -> System.out.printf("%s value is at position %d with the value of %.2f%n",
        names[c[0]++], (int)a[0] + 1, a[1]));
输出

Smallest value is at position 3 with the value of 0.01
2nd smallest value is at position 2 with the value of 0.12
3rd smallest value is at position 1 with the value of 0.34
4th smallest value is at position 4 with the value of 0.45
5th smallest value is at position 5 with the value of 0.65

当然,您可以将print语句放在“temp.add”位置,而不是使用ArrayList。或者考虑在temp上执行foreach循环。取决于你的情况,什么对你最合适。谢谢。但是我如何从原始数组中知道前5个值的位置呢?很抱歉,我误解了你的问题。我已经更正了我的代码示例,以便它能够显示您要查找的内容。
Smallest value is at position 3 with the value of 0.01
2nd smallest value is at position 2 with the value of 0.12
3rd smallest value is at position 1 with the value of 0.34
4th smallest value is at position 4 with the value of 0.45
5th smallest value is at position 5 with the value of 0.65