2015-08-26 114 views
0

有人能告訴我爲什麼我不斷得到IndexOutOfBoundsExceptionIndexOutOfBounds錯誤插入排序列表

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 

回答

0
import java.util.ArrayList; 

import java.util.Collections; 
import java.util.Comparator; 
import java.util.Random; 

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<>(); 
     ArrayList<Object> cc = new ArrayList<>(); 
     int count = arrayList.size(); 
      System.out.println(arrayList.toString()); 

     int i = 0; 

    String prv = ""; 
    int letterscount=0; 
     System.out.println(str.size()); 
     while (i < count) { 
       //System.out.println(str.size()); 
      // Alphabets alphabets = new Alphabets(arrayList.get(i).substring(0,1)); 
      String temp = arrayList.get(i).substring(0, 1); 

      if(!prv.equals(temp)) 
      { 

       str.add(temp); 

       int x = 0 ; 

       while (temp.equalsIgnoreCase(arrayList.get(x).substring(0, 1))) { 

        if(!prv.equalsIgnoreCase(temp)){ 
         str.add(arrayList.get(x)); 
        } 

        x++; 

       } 

       letterscount++; 
      } 

      if(!prv.equalsIgnoreCase(temp)){ 

        int size = str.size()-letterscount; 

        System.out.println(str.toString()); 
        str.add(arrayList.get(i)); 
        System.out.println(size); 

      } 
      prv = temp; 

      i++; 
     } 

     return str; 
    } 

} 


    class Cheeses { 
     static String[] sCheeseStrings = {"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"}; 

     public Cheeses() { 

     } 
} 

    class Alphabets { 
    String alp; 

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

    public String getAlp() { 
     return alp; 
    } 
} 
+0

關於如何解決這個問題的任何建議? –

+0

仍然是相同的錯誤,它看起來像在end while循環完成i == count的值時的結尾。 –

+1

在這個str.add(arrayList.get(i))中的錯誤;你試圖在循環後傳遞我 – A7med

1

看這個循環:

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)) 

(我沒試過跟隨你在做什麼的具體細節 - 只是解決眼前的問題)

+0

這didnt工作,我會期望新的ArrayList str的大小比arrayList大,但由於某種原因,它總是低於70, –

+0

線程「主」java.lang.IndexOutOfBoundsException異常:索引:70,大小:70我保持得到這個錯誤 –

+0

@JudeFernandes:它沒有幫助你沒有通過堆棧跟蹤顯示問題出在哪裏。你是否嘗試過通過代碼進行調試以確定問題出在哪裏? (我指出的問題至少有一個*問題,但當然可能有其他問題......) –