在Java中使用两个数组进行计算

我要使用这两个数组来显示高温和低温以及所有温度的平均值。对于整数,应该显示最低温度,然后显示最高温度。因此,对于数组中的每个两个整数,在另一个数组中要加上一年。我如何根据选择的年份使其仅显示两个整数?yearTemp用于ComboBox,并且2个整数将显示在不可编辑的文本框中。



MYYA
浏览 155回答 2
2回答

泛舟湖上清波郎朗

我希望将此作为评论,但是我还没有足够的声誉,所以我来回答。就个人而言,我将为此使用某种Map,其速度可能高达O(1),其中遍历数组时是O(N)(或O(N / 2)?)。但是这里是如何根据提供的数据进行高/低的计算。我不了解ComboBox界面。void select_year(int year_selected) {&nbsp; &nbsp; for(int i = 0 ; i < yearTemp.length ++i) {&nbsp; &nbsp; &nbsp; &nbsp; if((i* 2) + 1 > highLowTemp.length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw Exception("bad data"); // probably handle this better&nbsp; &nbsp; &nbsp; &nbsp; int year = yearTemp[i]&nbsp; &nbsp; &nbsp; &nbsp; if(year_selected == year) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int low = highLowTemp[i * 2]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int high = highLowTemp[(i * 2) + 1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this is the high and low for the year_selected&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do stuff with it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // edit: break from here when you are done&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java