J-Reid
J-Reid

Reputation: 5

lowest number in an array associated with a string array

I have searched all I could in regards to finding the lowest number in an array; however, I could not find any examples that had an array associated with a separate String array. I have two arrays, one an array of names, the second an array of times (int). I want a result that shows the lowest time and who it was that achieved that time.

This is the skeleton that I have to work with:

class Marathon {
    public static void main (String[] arguments){
        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
        };

        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i] + ": " + times[i]);
        }
    }
}

I was able to code something to get the lowest time:

public class Test {

    public static void main (String[] args) {

        String[] names = new String[] {"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"
        };

        int[] times = new int[]{ 341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265
        };

        int win = times[0];

        for(int i=0; i< names.length; i++) {
            if (times[i] < win) 
                win = times[i];                                     
        }    
        System.out.println("names[i]" + ": " + win);                
    }        
}
// result "names[i]: 243

but cannot seem to find a way to show the associated name (so I don't even know if this code is bringing me closer or further way). The closest I have come is from trying a continue loop... but it only compared directly with times[0] and I received a result of everything less than 341.

public class Marathon { 

    public static void main (String[] args) {

        String[] names = new String[] {"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"
        };

        int[] times = new int[] {341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265
        };

        for (int i = 1; i < names.length; i++) {
            if (times[i] >= times[0]) continue;
                System.out.println(names[i]+ ": " + times[i]);
        }
    }
}
//Result: 
//"Thomas: 273
//Hamilton: 278
//Suzie: 329
//Emma: 275
//John: 243
//James: 334
//Daniel: 299
//Aaron: 317
//Kate: 265"

I have also attempted to compare with times[i++] resulting in an error and am unsure of another way to compare with the entire array and keep the names associated with the correct times (I have found plenty of ways to disassociate the correct name and time). How can I associate the correct name with only the lowest time. The next stage will be showing the runner up as well; however, I am hoping I can figure that out once I get the first place person and time... The only examples I have found anywhere involve only one array, the multiple array thing seems to have thrown me for a loop!

Upvotes: 0

Views: 408

Answers (6)

Saravana
Saravana

Reputation: 12817

Try this

    String[] names = new String[] { "Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily",
            "Daniel", "Neda", "Aaron", "Kate" };
    int[] times = new int[] { 341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265 };

    int minIndex = IntStream.range(0, times.length).boxed().min((a, b) -> times[a] - times[b]).get();
    System.out.println(minIndex + " " + times[minIndex] + " " + names[minIndex]);

    TreeMap<Integer, String> sortedMap = IntStream.range(0, times.length).collect(TreeMap<Integer, String>::new,
            (map, i) -> map.put(times[i], names[i]), Map::putAll);
    System.out.println(sortedMap.firstEntry());

output

8 243 John
243=John

Upvotes: 0

BurntPieCrust
BurntPieCrust

Reputation: 30

Assuming the index of your name array is associated with the index of your time array

public static void main (String[] args) {

    String[] names = new String[] {"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"
    };

    int[] times = new int[]{ 341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265
    };

    int win = times[0];
    int winningIndex = 0;


    for(int i=0; i< names.length; i++) {

        if (times[i] < win){ 
            win = times[i];
            winningIndex = i;
        }
    }    
        System.out.println(names[winningIndex] + ": " + times[winningIndex]);                
 // result "John: 243
}   

Upvotes: 0

seal
seal

Reputation: 1143

You could put name and time in a class, create object and put that in ArrayList then sort that based on time. take the following code as example.

    public static void main(String[] args) {
    List<Node> list = new ArrayList<>(Arrays.asList(new Node("Elena", 341),
            new Node("Thomas", 273), new Node("Hamilton", 278)));

    Collections.sort(list);

    for (Node node : list) {
        System.out.println(node.name + " " + node.time);
    }
}

 // Implement Comparable or pass Comparator interface.
 // here I have used implementing interface. :)
static class Node implements Comparable<Node> {
    String name;
    int time;

    Node(String name, int time) {
        this.name = name;
        this.time = time;
    }

    @Override
    public int compareTo(Node o) { // will sort based on time
        return Integer.compare(this.time, o.time);
    }
}

in Java 8 style,

list.stream()
            .sorted()
            .forEach(i -> System.out.println(i.name + " " + i.time));

Upvotes: 0

DwB
DwB

Reputation: 38320

The real answer
Stop using clown solutions and start using objects. Create an object that holds name and time. Stop using arrays. Store the new object type in a List (LinkedList is probably fine, use ArrayList if you need random access into the List).

The answer you want
Using two arrays to store related data about one item is a wonderful solution. Simple solution to find the lowest time and the associated name:

  1. Find the index of the lowest time.
  2. Since the arrays are in the same order, use that index value to retrieve the name from the wonderfully separate name array.

Upvotes: 1

progyammer
progyammer

Reputation: 1508

Use a variable min to store the index in times[] that has the lowest value. Iterate over the array times once, since that's what determines the judgement. In the course of the iteration, check whether the current element in times is less than that at index min. If it is, assign the current index to min.

int min = 0;
for(int i=0; i<times.length; i++){
    if(times[i] < times[min])
        min = i;
}
System.out.println("Lowest time is of "+names[min]+
                   ", the time being "+times[min]);

Output:

Lowest time is of John, the time being 243

Upvotes: 0

roeygol
roeygol

Reputation: 5048

Find minimum value in array:

int[] array ...
int min = Integer.MAX_VALUE;
int index = -1;
for (int i=0 i<array.length; i++){

    if (array[i] > min){
         index = i;
         min = array[i];
    }
}
System.out.println(names[index]);

Upvotes: 0

Related Questions