java - 如何找到数组中元素的索引?

标签 java arrays

我正在尝试解决 this问题:

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
};

基本上有2个数组,一个是名字,一个是时间,数组索引是匹配的(例如Elena的时间是341),我要找到跑得最快的人,所以谁的时间最小就是最快的。

首先我找到了 times 数组中的最小值。

for (int i = 0; i < array.length; i++) {
    if(times[i] < fastest)
        fastest = times[i];
}

但我不知道如何匹配名称数组和时间数组,我试过了但是没有用

System.out.println(Arrays.asList(names).indexOf(fastest));

最佳答案

怎么样:

public class test {


public static void main(String[] args)
{

    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
            };

            int fastest = Integer.MAX_VALUE;
    int slowestRunnner = 0;

            for (int i = 0; i < times.length; i++) {
                if(times[i] < fastest)
                {
                    fastest = times[i];
                    slowestRunnner = i;
                }
            }

            System.out.println(names[slowestRunnner]);
}
}

System.out.println(names[slowestRunner]);

关于java - 如何找到数组中元素的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41025440/

相关文章:

java - 你如何命名你的版本?

python - np.array 的尺寸变化

java - 在二维数组中查找重复数字

Servlet 中变量的 Javascript 值未更新

java - 获得拥有 1000 个关注者的推特用户

JavaScript 函数不检索数组元素

c# - Asp.Net DataList 绑定(bind) ImageUrls 数组

c++ - 严格相同的数组输入

java - 我的 java public static void main(String []args) 和一个简单的 println 在 android studio 中不起作用

Javadoc @link 到 Kotlin 类