1

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));
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85

7 Answers7

4

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]);
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
java_doctor_101
  • 3,287
  • 4
  • 46
  • 78
  • thanks for the answer, it worked, I did `int slowestRunnner` first, I didn't set it to zero and got an error, can you explain why I should set it to any integer in advance? – Ege Kuzubasioglu Dec 07 '16 at 19:22
  • maybe i am just having a brain fart but the above code will always give you i = array.length -1 becuase you don't have brackets around the if statement therefore only fastest = times[i] will execute for the if and slowestRunner = i gets set every iteration of the loop...? – RAZ_Muh_Taz Dec 07 '16 at 19:29
  • A local variable must be explicitly given a value before it is used, by either initialization or assignment, in a way that can be verified by the compiler using the rules for definite assignment. – java_doctor_101 Dec 07 '16 at 19:33
  • @RAZ_Muh_Taz you don't need braces with if in this condition because compiler will automatically include everything inside the if condition. – java_doctor_101 Dec 07 '16 at 19:37
  • how does it know to include everything under the if? this isn't python so i wouldn't think the spaces identify them as being part of the if @nitinsh99 – RAZ_Muh_Taz Dec 07 '16 at 19:41
  • @RAZ_Muh_Taz I would suggest you to check out http://stackoverflow.com/questions/8020228/is-it-ok-if-i-omit-curly-braces-in-java – java_doctor_101 Dec 07 '16 at 19:51
  • it doesn't work, i just ran a simple example and it doesn't work, the no brackets only includes the first line after the if, every subsequent lines are not included and get called everytime inside the for loop, unless this is a java 8 only feature it won't work @nitinsh99 – RAZ_Muh_Taz Dec 07 '16 at 19:57
  • @RAZ_Muh_Taz I can guarantee it works. See the screenshot. – java_doctor_101 Dec 07 '16 at 19:58
  • No you're output is what i expected it's the last person in the array, that output is WRONG, the fastest person is John - 243 and NOT Kate who is the last person in the array and only has a time of 265 which would have been easy to see if you also print the fastest variable – RAZ_Muh_Taz Dec 07 '16 at 20:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130042/discussion-between-nitinsh99-and-raz-muh-taz). – java_doctor_101 Dec 07 '16 at 20:04
1
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

Daniel
  • 7,357
  • 7
  • 32
  • 84
1

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?

Ryan Gee
  • 394
  • 1
  • 9
0

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]);
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
0

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.

0

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.

Naman
  • 27,789
  • 26
  • 218
  • 353
0

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]);
hasham.98
  • 73
  • 3