// Find smallest year
int pastMin=0;
int currentMin=ontarioBankInfoInt.get(0);
int currentMinIndex=0;
int reps=(ontarioBankInfoInt.size()-1)/3;
for(int x=0; x<=reps; x++){
for (int i=0; i<ontarioBankInfoInt.size()-1; i++){
if (ontarioBankInfoInt.get(i)<currentMin){
if (ontarioBankInfoInt.get(i)>pastMin){
currentMin = ontarioBankInfoInt.get(i);
currentMinIndex = i;
pastMin = currentMin;
}//If End
}//If End
}//For End
//Add other information
yearArray.add(currentMin);
}//For End
注意:reps 是需要查找的年数。 这段代码的目的是在 arrayList 中搜索最小的年份,然后将其添加到“yearArray”中,然后找到下一个最小的添加将其添加到 say 数组中,冲洗并重复,直到它遍历整个数组。
我的问题是它总是在同一年返回,我不确定为什么有帮助?
谢谢
最佳答案
您没有重新初始化 currentMin
和 currentMinIndex
,因此它们将达到最小值,然后它们将永远不会被重新分配。
您需要在循环内重新初始化它们。
for(int x=0; x<=reps; x++){
int currentMin = Integer.MAX_VALUE; // Do this here.
//int currentMinIndex = -1; // This is never used??
for (int i=0; i < ontarioBankInfoInt.size() - 1; i++) {
int value = ontarioBankInfoInt.get(i);
if (value < currentMin && value > pastMin) {
currentMin = value;
//currentMinIndex = i;
}
}
pastMin = currentMin; // I also think you want this here.
yearArray.add(currentMin);
}
或者您可以对列表进行排序并获取第一个 reps
元素。
关于java - 通过ArrayLists排序,代码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11051329/