gpt4 book ai didi

java - 插入排序列表时出现 IndexOutOfBounds 错误

转载 作者:行者123 更新时间:2023-12-01 11:12:03 27 4
gpt4 key购买 nike

有人能告诉我为什么我总是收到 IndexOutOfBoundsException 吗?

Cheeses 只是一个包含一组字符串的数组。Alphabets 是一个包含单个字符串变量的类。

我正在尝试编写一个程序,其中字符串前面带有起始字母。

所以会有一个 A 后面跟着以字母 A 开头的单词,依此类推。

    public class RecyclerTest {

public static void main(String[] args) {
RecyclerTest obj = new RecyclerTest();
ArrayList<Object> arr_obj = obj.getRandomSublist(Cheeses.sCheeseStrings, 20);
// System.out.println(arr_obj.toString()+"");

}

ArrayList<Object> getRandomSublist(String[] array, int amount) {
final ArrayList<String> list = new ArrayList<>(amount);
Random random = new Random();
while (list.size() < amount) {
list.add(array[random.nextInt(array.length)]);
}
Collections.sort(list, new sortList());
return insert(list);
}

class sortList implements Comparator<String> {

@Override
public int compare(String s, String t1) {
return s.compareTo(t1);
}
}

ArrayList<Object> insert(ArrayList<String> arrayList) {
ArrayList<Object> str = new ArrayList<>();
int count = arrayList.size();
System.out.println(arrayList.toString());
int i = 0;
while (i < count) {
// Alphabets alphabets = new Alphabets(arrayList.get(i).substring(0,1));
String temp = arrayList.get(i).substring(0, 1);
str.add(temp);
System.out.println(str.toString());
System.out.println(i);
while ((i < count) && (temp.equalsIgnoreCase(arrayList.get(i).substring(0, 1)))) {
str.add(arrayList.get(i));
i++;
}
}
str.add(arrayList.get(i));
// System.out.println(str.size());
return str;
}

}



public class Alphabets {
String alp;

public Alphabets(String alp) {
this.alp = alp;
}

public String getAlp() {
return alp;
}
}

Original String

[Baby Swiss, Bresse Bleu, Bruder Basil, Charolais, Charolais, Coeur de Chevre, Cold Pack, Fourme de Montbrison, Fresh Mozzarella, Fromage Frais, Havarti, Le Fium Orbo, Loddiswell Avondale, Peekskill Pyramid, Remedou, Ricotta Salata, Samso, Tete de Moine, Trou du Cru, Tymsboro]

Output

[B]
0
[B, Baby Swiss, Bresse Bleu, Bruder Basil, C]
3
[B, Baby Swiss, Bresse Bleu, Bruder Basil, C, Charolais, Charolais, Coeur de Chevre, Cold Pack, F]
7
[B, Baby Swiss, Bresse Bleu, Bruder Basil, C, Charolais, Charolais, Coeur de Chevre, Cold Pack, F, Fourme de Montbrison, Fresh Mozzarella, Fromage Frais, H]
10
[B, Baby Swiss, Bresse Bleu, Bruder Basil, C, Charolais, Charolais, Coeur de Chevre, Cold Pack, F, Fourme de Montbrison, Fresh Mozzarella, Fromage Frais, H, Havarti, L]
11
[B, Baby Swiss, Bresse Bleu, Bruder Basil, C, Charolais, Charolais, Coeur de Chevre, Cold Pack, F, Fourme de Montbrison, Fresh Mozzarella, Fromage Frais, H, Havarti, L, Le Fium Orbo, Loddiswell Avondale, P]
13
[B, Baby Swiss, Bresse Bleu, Bruder Basil, C, Charolais, Charolais, Coeur de Chevre, Cold Pack, F, Fourme de Montbrison, Fresh Mozzarella, Fromage Frais, H, Havarti, L, Le Fium Orbo, Loddiswell Avondale, P, Peekskill Pyramid, R]
14
[B, Baby Swiss, Bresse Bleu, Bruder Basil, C, Charolais, Charolais, Coeur de Chevre, Cold Pack, F, Fourme de Montbrison, Fresh Mozzarella, Fromage Frais, H, Havarti, L, Le Fium Orbo, Loddiswell Avondale, P, Peekskill Pyramid, R, Remedou, Ricotta Salata, S]
16
[B, Baby Swiss, Bresse Bleu, Bruder Basil, C, Charolais, Charolais, Coeur de Chevre, Cold Pack, F, Fourme de Montbrison, Fresh Mozzarella, Fromage Frais, H, Havarti, L, Le Fium Orbo, Loddiswell Avondale, P, Peekskill Pyramid, R, Remedou, Ricotta Salata, S, Samso, T]
17

最佳答案

看看这个循环:

while ((alphabets.getAlp().equalsIgnoreCase(arrayList.get(i).substring(0, 1))) && (i < count)) {
str.add(arrayList.get(i));
i++;
}

注意您如何调用arrayList.get(i) 检查 i < count 之前...所以如果 i == count ,它会在那条线上爆炸。您也许可以逆转这些条件:

while (i < count && alphabets.getAlp().equalsIgnoreCase(arrayList.get(i).substring(0, 1))

(我没有尝试遵循您正在做的事情的确切细节 - 只是解决眼前的问题。)

关于java - 插入排序列表时出现 IndexOutOfBounds 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32233200/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com