IndexOutOfBounds error inserting into sorted list

Could someone tell me why i keep getting an IndexOutOfBoundsException?

Cheeses are just an array containing a set of strings. Alphabets is a class containing a single string variable.

I am trying to write a program where the strings are preceded by the starting alphabet.

So there would be an A followed by words starting with the letter A and so on.

    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
Jon Skeet
people
quotationmark

Look at this loop:

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

Note how you call arrayList.get(i) before you check for i < count... so if i == count, it will blow up on that line. You may just be able to reverse those conditions:

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

(I haven't tried to follow the exact details of what you're doing - just solve the immediate problem.)

people

See more on this question at Stackoverflow