添加到arraylist中的列表时IndexOutOfBoundsException

时间:2015-05-10 10:32:44

标签: java list arraylist indexoutofboundsexception

我正在制作一个程序,必须根据前两个字母对每个英文单词进行排序。每组两个字母都有自己的列表,我必须将这些字词添加到其中,因此我总共有676个列表。我尝试制作列表的ArrayList来执行此操作:

public static List<List<String>> englishWordList = new ArrayList<List<String>>(673);

现在当我尝试将元素添加到其中一个列表时,我得到一个IndexOutOfBoundsException

    private static void letterSort(String s){
    //Sorts the words by the first two letters and places in the appropriate list.
    String letterGet = s.substring(0,2);
    for(int i = 0; i < 676; i++){
        if(letterGet.equals(letterCombos[i])){
            Debug(s);
            Debug(letterGet);
            try{
                englishWordList.get(i).add(s); \\IndexOutOfBoundsException here
            }catch(Exception e){
                e.printStackTrace();
                System.exit(0);
            }
        }
    }

如果需要更多信息,我将非常感谢您对修复此问题的任何帮助。我将非常乐意添加它。

1 个答案:

答案 0 :(得分:0)

您只是初始化了#include<stdio.h> int priv_element = 88; int array[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int next_element = 99; main (int argc, char *argv[]) { int *ptr_to_first_element = &array[0][0]; int (*ptr_to_1d_arry)[5] = &array[0]; int (*ptr_to_2d_arry)[2][5] = &array; printf ("Print first num of first array of 2-Dim array: %d\n", *ptr_to_first_element); ptr_to_first_element += 5; printf ("Print first num of second array of 2-Dim array: %d\n", *ptr_to_first_element); printf ("Print first num of first array of 2-Dim array: %d\n", (*ptr_to_1d_arry)[0]); ptr_to_1d_arry++; printf ("Print first num of second array of 2-Dim array: %d\n", (*ptr_to_1d_arry)[0]); printf ("Print first num of first array of 2-Dim array: %d\n", (*ptr_to_2d_arry)[0][0]); ptr_to_2d_arry++; printf ("Now you increased to point end of 2d-array space. So next to it is next_element on data-seg: %d\n", (*ptr_to_2d_arry)[0][0]); } ,但您没有添加任何内容。因此englishWordList为空,任何englishWordList调用都会引发get(int)

您可以通过以下方式填充空IndexOutOfBoundsException(另请注意,您将初始容量设置为673而不是676):

List