【发布时间】:2018-03-29 21:17:21
【问题描述】:
我在编写一个 for-each 循环时遇到了麻烦,该循环搜索 arraylist 并返回大陆内具有最高 gdp 的县名。这是我现在的代码。 (ElementsList就是原来的ArrayList)
public Country highestGdp(String continent) {
boolean flag;
for (Country cont : ElementsList) {
if (cont.getContinent().equals(continent)) {
ArrayList<Country> TMP1 = new ArrayList<Country>();
TMP1.add(cont);
for (Country gdp : TMP1) {
double max = 0;
if (max < gdp.getGDP()) {
max = gdp.getGDP();
}
if (gdp.getGDP() == max) {
ArrayList<Country> TMP2 = new ArrayList<Country>();
TMP2.add(gdp);
}
return gdp;
}
}
}
return null;
}
【问题讨论】:
-
您应该尝试在两个单独的循环中执行此操作,或者在每次找到新国家/地区时检查最大值。不需要有两个嵌套循环。