【发布时间】:2017-03-19 04:05:00
【问题描述】:
我必须为学校的事情调整选择排序。目的是返回玩家的排名,获胜者优先等。排名相同的玩家应该按照他们在锦标赛列表中出现的顺序列出。
public ArrayList<Player> ranking() {
ArrayList<Player> result = new ArrayList<Player>();
// Supply this code!
return result;
现在这是我调整选择排序的尝试
int smallInt = 0;
int j=0;
int smallIntIndex = 0;
for(int i=1;i<players.size();i++){
smallInt = result.get(i-1).totalScore();
smallIntIndex = i-1;
for(j=i;j<players.size();j++){
if(result.get(j).totalScore()<smallInt){
smallInt = result.get(j).totalScore();
smallIntIndex = j;
}
}
int temp = result.get(smallIntIndex).totalScore();
result.set(smallIntIndex, result.get(i-1));
result.set(i-1, temp);
}
return result;
唯一让我出错的是最后一行
result.set(i-1, temp); //The method set(int, Player) in the type
//ArrayList<Player> is not applicable for the arguments (int, int)
知道我做错了什么吗?大部分改编是正确的吗?任何帮助表示赞赏。谢谢
附言请不要建议比较器或类似的东西,这不是我要找的。p>
【问题讨论】:
-
错误说明了一切。将变量
temp更改为Player类型,而不是int。 -
外循环必须从索引
0开始,NOT1,并且必须在players.size() - 1结束,内循环必须从索引i+1开始。 en.wikipedia.org/wiki/Selection_sort#Implementation
标签: java arraylist selection-sort