如何获取数组中最大值的索引?

时间:2021-05-20 22:57:19

I have two arrays

我有两个数组

String[] city;
int[] temp;

they both have a length of 4. City holds the name of each city, and temp holds the average temperature of each city. The temps in temp are in the same order as the cities in city. So my question is, how can I get the index of the max int in temp? I want to print out

它们的长度均为4.城市拥有每个城市的名称,而温度则保持每个城市的平均温度。临时温度与城市中的城市顺序相同。所以我的问题是,如何获得temp中max int的索引?我想要打印出来

"The city with the highest average temperature is " + city[index] + 
" with an average temperature of " + temp[max];

I thought of taking the index of the max int in city, and plugging that into city[]. Since the arrays are the same size and in the same order, I just need to be able to return the index of the max value in int[] temp;

我想到了在城市中获取max int的索引,并将其插入city []。由于数组大小相同且顺序相同,我只需要能够返回int [] temp中最大值的索引;

4 个解决方案

#1


3  

If you do not want to use another class you can do the below. You need to store both the max and the index of where max was so that you can also print the city

如果您不想使用其他课程,可以执行以下操作。您需要存储max的最大值和索引,以便您还可以打印城市

String[] city = getCities ();  // assuming this code is done
int[] temp = getTemparatures ();
int max = Integer.MIN_VALUE;
int index = -1;

for(int i = 0; i < temp.length; i ++){
    if(max < temp[i]){
        max = temp[i];
        index = i;
    }
}

System.out.println ("The city with the highest average temperature is " + city[index] + 
" with an average temperature of " + temp[index]);

#2


-1  

You can get the length of an array using <name>.length. The length of the array is one more than the highest index so you should use 'name[name.length - 1]` to get the item with the highest index.

您可以使用 .length获取数组的长度。数组的长度比最高索引多一个,所以你应该使用'name [name.length - 1]`来获得索引最高的项。

#3


-2  

You could iterate through the temp array, find the max and store the value a variable, and use that variable in your output statement. Below is some code, hope this helps

您可以遍历temp数组,找到max并将值存储为变量,并在输出语句中使用该变量。下面是一些代码,希望这有帮助

String[] city;
int[] temp;
int max = 0;

for(int i = 0; i < temp.size; i ++){
    if(max < temp[i]){
        max = temp[i];
    }
}

"The city with the highest average temperature is " + city[max] + 
" with an average temperature of " + temp[max];

#4


-2  

Sorry, I wrote in this editor, but the essence is the same

对不起,我在这个编辑器中写道,但实质是一样的

int max = -100;
for (int i = 0; i < temp.length; i++){
    if (temp[i] > max){
        max = temp[i];
    }
}
System.out.print("The city with the highest average temperature is " + city[your index] + 
" with an average temperature of " + max);

#1


3  

If you do not want to use another class you can do the below. You need to store both the max and the index of where max was so that you can also print the city

如果您不想使用其他课程,可以执行以下操作。您需要存储max的最大值和索引,以便您还可以打印城市

String[] city = getCities ();  // assuming this code is done
int[] temp = getTemparatures ();
int max = Integer.MIN_VALUE;
int index = -1;

for(int i = 0; i < temp.length; i ++){
    if(max < temp[i]){
        max = temp[i];
        index = i;
    }
}

System.out.println ("The city with the highest average temperature is " + city[index] + 
" with an average temperature of " + temp[index]);

#2


-1  

You can get the length of an array using <name>.length. The length of the array is one more than the highest index so you should use 'name[name.length - 1]` to get the item with the highest index.

您可以使用 .length获取数组的长度。数组的长度比最高索引多一个,所以你应该使用'name [name.length - 1]`来获得索引最高的项。

#3


-2  

You could iterate through the temp array, find the max and store the value a variable, and use that variable in your output statement. Below is some code, hope this helps

您可以遍历temp数组,找到max并将值存储为变量,并在输出语句中使用该变量。下面是一些代码,希望这有帮助

String[] city;
int[] temp;
int max = 0;

for(int i = 0; i < temp.size; i ++){
    if(max < temp[i]){
        max = temp[i];
    }
}

"The city with the highest average temperature is " + city[max] + 
" with an average temperature of " + temp[max];

#4


-2  

Sorry, I wrote in this editor, but the essence is the same

对不起,我在这个编辑器中写道,但实质是一样的

int max = -100;
for (int i = 0; i < temp.length; i++){
    if (temp[i] > max){
        max = temp[i];
    }
}
System.out.print("The city with the highest average temperature is " + city[your index] + 
" with an average temperature of " + max);