简体   繁体   中英

How do I compare an arrays set of elements and return the desired elements

Im building a program and it has to take a set of information (Runners and their times) and make a method that will compare the times of all runners. Im a beginner and don't know much and have to stick to what I know. I need to get the lowest runners in the 2017 year please lead me in the right direction. (I want to learn but not get the answer!!!) Thanks!

Ive tried to compare all the integers of the array but that seemed long and also wrong

public static void main(String[] args) {
    int runnersTime2017 [] = {357, 299, 432, 326, 275, 450, 265, 343, 264, 308, 242, 377, 273};
    int runnersTime2018 [] = {341, 307, 328, 283, 274, 359, 256, 323, 269, 308, 249, 340, 238};
    fastestRunner2017(runnersTime2017);


 }

public static int fastestRunner2017(int Array1 []){ //This is what I am trying to do


    if (Array1[1] <= Array1[]){

        System.out.println(T1);

        return T1;

    }
    //This is what I did before and seemed wrong
    else if (T2 <= T1 && T2 <= T3 && T2 <= T4 && T2 <= T5 && T2 <= T6 && T2 <= T7 && T2 <= T8 && T2 <= T9 && T2 <= T10 && T2 <= T11 && T2 <= T12 && T2 <= T13){

        System.out.println(T2); 

        return T2;
    }    

    else if (T3 <= T1 && T3 <= T2 && T3 <= T4 && T3 <= T5 && T3 <= T6 && T3 <= T7 && T3 <= T8 && T3 <= T9 && T3 <= T10 && T3 <= T11 && T3 <= T12 && T3 <= T13){

        System.out.println(T3);

        return T3;
    }

    else if (T4 <= T1 && T4 <= T2 && T4 <= T3 && T4 <= T5){
        System.out.println(T4);

        return T4;
    }

    else {

        System.out.println(T13);
        return T13;

    }

I want to compare one part of the array to the rest of them

The most straight-forward way to solve this would be a for-loop .

If I'm understanding your case here, you would take each value of the first array and compare it to each value of the second array. If that first array value is greater than each, print it. Otherwise take the greater value. Something like:

foundValue = runnersTime2017[0];
for (int i = 0; i < runnersTime2017.length; ++i) {
    for (int j = 0; j < runnersTime2018.length; ++j) {
        if (runnersTime2018[j] > runnersTime2017[i]) {
            foundValue = runnersTime2018[j];
         }
    }
}
return foundValue;

Note that this is also a relatively inefficient method (O(n^2)), but for a beginner it should suffice without going over your head.

How about sort the Int array and take the first or last value

     int runnersTime2017 [] = {357, 299, 432, 326, 275, 450, 265, 343, 264, 308, 242, 377, 273};

        int runnersTime2018 [] = {341, 307, 328, 283, 274, 359, 256, 323, 269, 308, 249, 340, 238};

       List<Integer> allRunner= new ArrayList<>(runnersTime2017.length+runnersTime2018.length);

     for(int r7 :runnersTime2017) {
         allRunner.add(r7);

     }
     Collections.sort(allRunner);
     //this prints the 2017 runners after storting
     System.out.println(allRunner);
     for(int r8 :runnersTime2017) {
         allRunner.add(r8);
     }
     //now sort again after adding 2018 runners  
     Collections.sort(allRunner);
     System.out.println(allRunner);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM