有人能告訴我爲什麼我不斷得到IndexOutOfBoundsException
?IndexOutOfBounds錯誤插入排序列表
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;
}
}
原始字符串
【寶貝瑞士,佈雷斯布魯,布魯德羅勒,夏洛萊,利木 夏洛萊,利木心德CHEVRE,冰袋,Fourme德蒙布里鬆,新鮮 芝士,鮮奶酪,哈瓦蒂乾酪,樂Fium ORBO,Loddiswell埃文代爾, 皮克斯基爾金字塔,Remedou,芝士薩拉塔,薩姆索島,太特省德莫因,特魯 杜酒莊,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
關於如何解決這個問題的任何建議? –
仍然是相同的錯誤,它看起來像在end while循環完成i == count的值時的結尾。 –
在這個str.add(arrayList.get(i))中的錯誤;你試圖在循環後傳遞我 – A7med