IndexOutOfBounds插入排序列表时出错

时间:2015-08-26 17:39:58

标签: java sorting exception arraylist

有人可以告诉我为什么我会继续获得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;
    }
}
  

原始字符串

     

[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金字塔,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

2 个答案:

答案 0 :(得分:1)

看看这个循环:

arrayList.get(i)

请注意,在检查i < count之前,您如何致电i == count ... ...如果while (i < count && alphabets.getAlp().equalsIgnoreCase(arrayList.get(i).substring(0, 1)) ,它将在该行上爆炸。你可能只能扭转这些条件:

while Matcher.find() {
  call Matcher.appendReplacement()
}
call Matcher.appendTail()

(我没有尝试按照你正在做的事情的具体细节 - 只解决眼前的问题。)

答案 1 :(得分: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;
    }
}