与字符串数组关联的数组中的最小数字

lowest number in an array associated with a string array

我已经尽我所能寻找数组中的最小数字;但是,我找不到任何具有与单独的 String 数组关联的数组的示例。我有两个数组,一个是名称数组,第二个是时间数组 (int)。我想要一个显示最短时间的结果以及达到那个时间的人。

这是我必须使用的骨架:

class Marathon {
    public static void main (String[] arguments){
        String[] names ={"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"
        };

        int[] times ={341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299,343, 317, 265
        };

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

我能够编写一些代码以获得最少的时间:

public class Test {

    public static void main (String[] args) {

        String[] names = new String[] {"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"
        };

        int[] times = new int[]{ 341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265
        };

        int win = times[0];

        for(int i=0; i< names.length; i++) {
            if (times[i] < win) 
                win = times[i];                                     
        }    
        System.out.println("names[i]" + ": " + win);                
    }        
}
// result "names[i]: 243

但似乎找不到显示关联名称的方法(所以我什至不知道这段代码是让我更近还是更远)。我最接近的是尝试继续循环......但它只直接与 times[0] 进行比较,我收到的所有结果都小于 341.

public class Marathon { 

    public static void main (String[] args) {

        String[] names = new String[] {"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"
        };

        int[] times = new int[] {341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265
        };

        for (int i = 1; i < names.length; i++) {
            if (times[i] >= times[0]) continue;
                System.out.println(names[i]+ ": " + times[i]);
        }
    }
}
//Result: 
//"Thomas: 273
//Hamilton: 278
//Suzie: 329
//Emma: 275
//John: 243
//James: 334
//Daniel: 299
//Aaron: 317
//Kate: 265"

我也曾尝试与 times[i++] 进行比较导致错误,并且不确定是否有另一种方法可以与整个数组进行比较并使名称与正确的时间相关联(我已经找到了很多方法来解除关联正确的姓名和时间)。我如何才能将正确的名称与最低时间相关联。下一阶段还将展示亚军;但是,我希望一旦我获得第一名的人和时间,我就能弄清楚这一点……我在任何地方找到的唯一例子只涉及一个数组,多数组的东西似乎让我陷入了循环!

查找数组中的最小值:

int[] array ...
int min = Integer.MAX_VALUE;
int index = -1;
for (int i=0 i<array.length; i++){

    if (array[i] > min){
         index = i;
         min = array[i];
    }
}
System.out.println(names[index]);

使用变量 min 存储 times[] 中具有最低值的索引。遍历数组 times 一次,因为这是决定判断的。在迭代过程中,检查times中的当前元素是否小于索引min中的元素。如果是,则将当前索引分配给 min

int min = 0;
for(int i=0; i<times.length; i++){
    if(times[i] < times[min])
        min = i;
}
System.out.println("Lowest time is of "+names[min]+
                   ", the time being "+times[min]);

输出:

Lowest time is of John, the time being 243

真正的答案
停止使用小丑解决方案并开始使用对象。 创建一个包含名称和时间的对象。 停止使用数组。 将新对象类型存储在列表中(LinkedList 可能没问题,如果需要随机访问列表,请使用 ArrayList)。

你想要的答案
使用两个数组来存储一个项目的相关数据是一个很好的解决方案。 查找最短时间和关联名称的简单解决方案:

  1. 找到时间最短的索引。
  2. 由于数组的顺序相同,因此使用该索引值从完全独立的名称数组中检索名称。

假设你的名字数组的索引与你的时间数组的索引相关联

public static void main (String[] args) {

    String[] names = new String[] {"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"
    };

    int[] times = new int[]{ 341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265
    };

    int win = times[0];
    int winningIndex = 0;


    for(int i=0; i< names.length; i++) {

        if (times[i] < win){ 
            win = times[i];
            winningIndex = i;
        }
    }    
        System.out.println(names[winningIndex] + ": " + times[winningIndex]);                
 // result "John: 243
}   

您可以将姓名和时间放入 class,创建对象并将其放入 ArrayList,然后根据 time 对其进行排序。以下面的代码为例。

    public static void main(String[] args) {
    List<Node> list = new ArrayList<>(Arrays.asList(new Node("Elena", 341),
            new Node("Thomas", 273), new Node("Hamilton", 278)));

    Collections.sort(list);

    for (Node node : list) {
        System.out.println(node.name + " " + node.time);
    }
}

 // Implement Comparable or pass Comparator interface.
 // here I have used implementing interface. :)
static class Node implements Comparable<Node> {
    String name;
    int time;

    Node(String name, int time) {
        this.name = name;
        this.time = time;
    }

    @Override
    public int compareTo(Node o) { // will sort based on time
        return Integer.compare(this.time, o.time);
    }
}

在Java8种风格中,

list.stream()
            .sorted()
            .forEach(i -> System.out.println(i.name + " " + i.time));

试试这个

    String[] names = new String[] { "Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily",
            "Daniel", "Neda", "Aaron", "Kate" };
    int[] times = new int[] { 341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265 };

    int minIndex = IntStream.range(0, times.length).boxed().min((a, b) -> times[a] - times[b]).get();
    System.out.println(minIndex + " " + times[minIndex] + " " + names[minIndex]);

    TreeMap<Integer, String> sortedMap = IntStream.range(0, times.length).collect(TreeMap<Integer, String>::new,
            (map, i) -> map.put(times[i], names[i]), Map::putAll);
    System.out.println(sortedMap.firstEntry());

输出

8 243 John
243=John