有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何显示索引而不是元素

嗨,我试图显示数组的索引,而不是元素。到目前为止,这是我代码的一部分:

int randomInt2 = randomGenerator.nextInt(15)+1;
double [] distances = new double [randomInt2];

Arrays.sort(distances);// sorts array from highest to lowest        
for (double val : distances) {
System.out.println("["+val+"],");
}

System.out.println("The Nearest to point 1 is point: ");

每个索引都有一个介于1-1000之间的值,但我不想显示我想显示的值,这样我就可以显示哪些索引最接近点1(即0) 对不起,如果我不清楚,如果你需要我解释更多,我很乐意


共 (5) 个答案

  1. # 1 楼答案

    我假设您想找出原始数组中距离最小的项的索引。在对数组排序时,这些信息会丢失,因为它只会从0..length-1索引。因此,你的答案永远是0

    你可以做两件事:
    1) 找到最小值:

    double[] distances = new double [randomInt2];
    // Need copy to for sorting
    double[] c = Arrays.copyOf(distances, distances.length);
    // Find minimum value
    Arrays.sort(c);
    double min = c[0];
    
    // Search that value in the original array:
    for (int i = 0; i < distances.length; ++i) {
      if (distances[i] == min) {
        System.out.println("Minimum distance at: " + i);
        break;
      }
    }
    

    2)将索引信息与距离信息一起存储:
    为此,您需要:

    1. 编写自己的类,比如public class Distance,带有距离和索引成员
    2. 实现一个^{},这样就可以按距离比较实例
    3. 制作一个类似Distance[] convertToDistance(double[] array)的函数,从纯双精度值创建一个距离数组(如果需要)
    4. 使用Arrays.sort(T[], Comparator<? extends T>)方法对数组进行排序
    5. sortedDistances[0].index获取结果
  2. # 2 楼答案

    多亏dasblinkenlight的评论,我终于明白了你需要什么

    最简单的方法就是创建一个类似

    class Value implements Comparable<Value> {
    
      int index;
      dobule value;
    
      public int compareTo(Value val) {
        return Double.compare(this.value, val.value);
      }
    }
    

    并使用它来存储值。注意Comparable实现——允许您使用Collections.sort()Arrays.sort()

    您也可以不使用排序。排序数组比查找最小值要复杂得多。只需在数组上迭代一次,找到最小值并返回其索引

    double minVal = Double.MAX_VALUE;
    int minIndex = -1;
    for (int i=0, max=distances.length; i<max;i++) {
        if (values[i] < minVal) {
            minVal = values[i];
            minIndex = i;
        }
    }
    System.out.println("The Nearest to point 1 is point: "+minIndex+" with value "+minVal);
    

    至于索引:如果要访问给定元素的索引,则不能使用标准foreach循环。其中一个原因是,您可能迭代的某些集合不支持元素排序(例如^{

    您必须使用标准的for循环或自己跟踪索引

    for (int i=0, max=distances.length; i<max;i++) {
        System.out.println("["+i+"] "+distances[i]);
    }
    

    或者

    int i = 0;
    for (double val : distances) {
        System.out.println("["+i+"] "+val);
        i++;
    }
    
  3. # 3 楼答案

    你可以让每一段距离都成为你的目标

    class Distance {
      public Distance(double dist) {
        this.dist = dist;
        this.id = idcount++;
      }
    
      static int idcount = 0;
      public int id;
      public double dist;
    }
    

    然后每次都打电话给身份证

  4. # 4 楼答案

    您可以使用老式的for循环,而不是增强的for循环:

    Arrays.sort(distances);// sorts array from highest to lowest        
    for (int i = 0; i < distances.lengh; ++i) {
        System.out.println("index: " + i + ":[" + val + "],");
    }
    
  5. # 5 楼答案

    试试这个

    for (int i=0;i< distances.length;i++) {
          System.out.println("distances["+i+"]");
    }