【发布时间】:2017-05-02 01:15:56
【问题描述】:
我有两个数组
String[] city;
int[] temp;
它们的长度都是 4。city 保存了每个城市的名称,temp 保存了每个城市的平均温度。 temp 中的温度与 city 中的城市的顺序相同。所以我的问题是,我怎样才能在 temp 中获得最大 int 的索引?我要打印出来
"The city with the highest average temperature is " + city[index] +
" with an average temperature of " + temp[max];
我想到了获取 city 中最大 int 的索引,并将其插入 city[]。由于数组的大小和顺序相同,我只需要能够返回 int[] temp 中的最大值的索引;
【问题讨论】:
-
如果你想知道最大值的索引,你可以遍历数组找到最大值并记住索引。或使用@Alex 的建议来使用 NumberUtils。旁注,设计问题:呃......你为什么不把临时和城市“绑定”在一起成为一个类......?像
city类。那么该类包含城市名称和温度..? -
@ScaryWombat 我不能对这个问题发表评论,因为我是新手,但我有一个问题要问。您能否通过创建一个包含整数和字符串的对象来扩展您的意思?
-
public class CityTemp { String city; int temp}CityTemp [] = new CityTemp [201];这很粗糙