简体   繁体   中英

How to get the second highest value from an array, the third highest value etc

I really need some help with this problem. I'm trying to write a function in Java code, but I'm stuck and I don't know how to continue. I have this data containing 1 row (area) and 7 columns (hospitals). For the area there is a percent that this person will end up at hospital 1, another percent it will end up at h2 etc. The data looks something like this:

hospital  h1  h2  h3 ...
area   
a1        11  45  3  ...

I have saved the data in a double array containing 7 values. I have this random generator fcn that randomly selects an index value of where in the array we are. If this random generator selects eg index=0 that would mean 11%, if it randomly selects index=2 that would mean 3% etc.

The problem is that after I have done this I want to check whether or not this hospital is idle or not. If it is idle then I want to return the randomly chosen integer index value. If it's not idle I want to choose the next hospital with the second highest percent and if that hospital is idle then return this int index value, otherwise continue and check the third highest percent and do another check and so on. If all hospitals are not idle then return the randomly chosen value.

This is what I've done so far:

double vector[] = new double[distrToEachHosp.length]();

//start by setting the contents to index
for(int index=0; index<vector.length; index++){
        vector[index] = index;
}
for(int i=0; i <= vector.length; i++){
    for(int x=1; x <= vector.length; x++){
        if (distrToEachHosp[vektor[x]] > distrToEachHosp[vektor[x+1]]){ //compare content that corresponds to index
            //move index
            int temp = vector[x];
            vector[x] = vector[x+1];
            vector[x+1] = temp;
        }
    }         
}
return vektor;


int rndValue = randomGenerator(distrToEachHosp);

for(int i=0; i<vector.length; i++){
    if(hospital.get(i).namn == hospitalNameVariable[rndValue] ){  //hospitalNameVariable is a variable containing the names of the hospitals
        if(hospital.get(i).idle()>0){
    return rndValue;                
    } else {
                if(vector[0] >= rndValue) {
        return vector[0];
        } else if(vektor[1] >= slumpVärde) {
            ......
      } 

 }

 }

And this is where I get stuck. I now I get some sorting of the data with the first algorithm, but logically I don't know how to include all the cases.

You can use Arrays.sort(vector) for sorting rather than implementing.

And for the second and third values you can always access via indexes like vector[vector.length-2] , vector[vector.length-3] respectively.

No reason to sort the array every time you pick a hospital, which seems like what you are trying to do.

If you sort the array one time at the beginning of your program, then its very easy. Set index to random choice, if its not idle then decrement your index, if its not idle then decrement your index, repeat until you find idle or run out of options. Since array is always in sorted order you always know next best choice is the previous value in the array, no matter where you are. Hth

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