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));
3
  • 1
    Why not just keep track of the index and use that when you loop? Commented Dec 7, 2016 at 19:12
  • use HashMap instead ? Commented Dec 7, 2016 at 19:15
  • 1
    tried putting System.out.println(names[i]); in for loop, in this case the last output would be the fastest but I just want only 1 output Commented Dec 7, 2016 at 19:16

7 Answers 7

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]);
Sign up to request clarification or add additional context in comments.

10 Comments

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?
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...?
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.
@RAZ_Muh_Taz you don't need braces with if in this condition because compiler will automatically include everything inside the if condition.
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
|
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?

Comments

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

Comments

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

Comments

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.

Comments

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.

4 Comments

Rather names[indexOfFastest] than Arrays.asList(names).get(indexOfFastest)
@Dukeling sure. please throw some light on why?
I guess I could say something about writing more code which calls 2 methods and constructs a class, and ultimately just ends up running the same code as the former on top of that, but I guess it's more just a best-practices issue.
@Dukeling Okay :) I get that.
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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.