FIL*_*IaS 0 java sorting arraylist
我正在尝试使用字符串(PlayersNames)和imageIcons(PlayersIcons)对ArrayList进行排序,这些值基于我存储在具有整数(结果)的其他arrayList中的值.正如你所看到我得到一个indexOutOfBoundsException,但我不明白为什么.也许早上的收获让我不会看到简单的事情.
ArrayList<String> PlayersNames=new ArrayList<String>;
ArrayList<ImageIcon> PlayersIcons=new ArrayList<ImageIcons>;
public void sortPlayers(ArrayList<Integer> results){
String tmp;
ImageIcon tmp2;
for (int i=0; i<PlayersNames.size(); i++) {
for (int j=PlayersNames.size(); j>i; j--) {
if (results.get(i) < results.get(i+1) ) { //IndexOutOfBoundsException!
tmp=PlayersNames.get(i+1);
PlayersNames.set(i+1,PlayersNames.get(i));
PlayersNames.set(i,tmp);
tmp2=PlayersIcons.get(i+1);
PlayersIcons.set(i+1,PlayersIcons.get(i));
PlayersIcons.set(i,tmp2);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当循环到达arrayList的末尾时,您试图获取一个超出列表末尾的项目.在这一行:
if (results.get(i) < results.get(i+1) ) {
Run Code Online (Sandbox Code Playgroud)
如果i = 9,使用包含10个项目的arrayList,results.get(9)将为您提供列表中的最后一项.results.get(10)将尝试获得不存在的东西.
| 归档时间: |
|
| 查看次数: |
2850 次 |
| 最近记录: |