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

时间:2016-12-07 19:10:22

标签: 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),我必须找到最快的跑步者,所以拥有最小时间的人是最快的。

首先我在时间数组中找到了最小值。

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

但我不知道如何将名字数组与时间数组匹配,我尝试了这个但它没有用

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

7 个答案:

答案 0 :(得分:4)

怎么样:

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]);

答案 1 :(得分:1)

int minimum = 0;
for(int i = 1; i < times.length; i++){
    if(times[minimum] > times[i]){
        minimum = i;
    }
}
System.out.println(names[minimum]);

这应该做的工作

答案 2 :(得分:1)

你能做到:

var fastest = '';
var fastestIndex = '';

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

然后使用:

names[fastestIndex]

得到名字?

答案 3 :(得分:0)

这样调用array_variable [index]

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

答案 4 :(得分:0)

这种情况最简单的方法:

with.dt = function(dt, expr){
  for (j in 1:length(expr)) set(dt, , names(expr)[j], dt[, eval(expr[[j]])])
}

test = data.table(1:10, 1:10, 1:10, 1:10)
with.dt(test, expression(
  V1 = V1^2,
  V2 = V1*V2,
  V3 = V2/V3,
  V4 = sqrt(V3),
  new = letters[V4]
))

#     V1   V2  V3 V4 new
# 1:   1    1   1  1   a
# 2:   4    8   4  2   b
# 3:   9   27   9  3   c
# 4:  16   64  16  4   d
# 5:  25  125  25  5   e
# 6:  36  216  36  6   f
# 7:  49  343  49  7   g
# 8:  64  512  64  8   h
# 9:  81  729  81  9   i
#10: 100 1000 100 10   j

但如果你使用Map,它会更好,它包含一对名字和数字。

答案 5 :(得分:0)

只需使用字段跟踪索引:

int indexOfFastest = 0;
int fastest = Integer.MAX_VALUE; // this initialization makes sure first element is assigned to fastest within the iteration
for (int i = 0; i < array.length; i++) {
    if(times[i] < fastest) {
        fastest = times[i];
        indexOfFastest = i;
    }
}

并进一步修改现有代码

System.out.println(Arrays.asList(names).get(indexOfFastest));

编辑 - 由于转换为List的代码最终会运行与在索引处获取数组元素值相同的评估。请更喜欢使用

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

代替更好的做法。

答案 6 :(得分:0)

它对我有用

int fastest=0;
        for (int i = 1; i < times.length; i++) {
            if(times[i] < times[fastest])
                fastest = i;}
        System.out.println("Fastest runner is "+names[fastest]);