Ege Kuzubasioglu
Ege Kuzubasioglu

Reputation: 6282

How to find the index of an element in an array?

I'm trying to solve this question:

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

basically there are 2 arrays, one for the names and one for the times, array indexes are matching (for example Elena's time is 341), I have to find the fastest runner, so whoever has the smallest time is the fastest.

first I found the smallest value in times array.

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

but I don't know how to match names array with times array, I tried this but it didn't work

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

Upvotes: 1

Views: 233

Answers (7)

java_doctor_101
java_doctor_101

Reputation: 3357

How about:

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

Upvotes: 4

hasham.98
hasham.98

Reputation: 73

It worked for me

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

Upvotes: 0

Naman
Naman

Reputation: 31878

Simply use a field to track the index as :

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

and further modify your existing code as

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

Edit - Since the code with converting into a List ends up running the same evaluation as getting the values of array element at an index. Please prefer using

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

instead for better practices.

Upvotes: 0

Daniel
Daniel

Reputation: 7724

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

this should do the job

Upvotes: 1

Vadim Beskrovnov
Vadim Beskrovnov

Reputation: 3

The simplest way for this case:

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

But it will be better, if you use Map, that contains pair of name and number.

Upvotes: 0

Ryan Gee
Ryan Gee

Reputation: 404

Could you do:

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

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

Then use:

names[fastestIndex]

to get the name?

Upvotes: 1

Md. Nasir Uddin Bhuiyan
Md. Nasir Uddin Bhuiyan

Reputation: 1596

call this way 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]);

Upvotes: 0

Related Questions