迭代Java中的列表时出现严重问题[indexOutOfBounds]

时间:2013-10-22 22:17:49

标签: java list indexoutofboundsexception

我正在编写代码来制作一个应用了许多计算语言学原理的程序。我现在的问题是下面的一段代码构成了一个“灵活化两个定义”的方法。 也就是说,它比较了同一个单词的两个不同定义,并且在每个定义中,空格或空格将被添加到稍后使用更改的定义(添加了空格)。
假设我们有以下两个定义,定义术语“自由落体”。

1) Free fall descent  of a body subjected only to            the   action of  gravity.
2) Free fall movement of a body in        a    gravitational field under  the influence of gravity

有一个名为stoplist的单词列表,其中包含单词:“of”,“a”,“in”,“to”和“under”。在该过程之后,定义中也包含在停止列表中的每个单词必须对应于空白空间或另一个定义的另一个停止列表单词。 因此,在执行此类过程之后,以两个不同列表表示的先前定义应如下所示:

1) Free fall descent  of a body ____ ____ subjected     only  to     the action    of gravity.
2) Free fall movement of a body in   a    gravitational field under  the influence of gravity.

我为实现此目的而编写的代码如下:


[...]
    String[] sList = STOPLIST.split(" ");  //this is the stoplist
    String[] definition1 = defA1.split(" ");  //this is the array of words of the first definition
    String[] definition2 = defA2.split(" ");  //this is the array of words of the second definition
    List<String> def1 = new ArrayList<String>();  
    List<String> def2 = new ArrayList<String>();
    List<String> stopList = new ArrayList<String>();

    for(String word : definition1){
         def1.add(word); //I transform arrays into lists this way because I used to think that using .asList() was the problem.
    }
    for(String word : definition2){
         def2.add(word);
    }
    for(String word : sList){
        stopList.add(word);
    }

    int mdef = (def1.size() <= def2.size()) ? def1.size() : def2.size(); //here mdef will have the value of the lenght of the shortest definition, and we are going to use the value of mdef to iterate later on.

    for(int i = 0; i < mdef; i++){
        if (stopList.contains(def1.get(i))) {  //here I check if the first word of the first definition is also found in the stoplist.
            if (!stopList.contains(def2.get(i))) {  //If the word of def1 previously checked is in the stoplist, as well as the corresponding word in the second definition, then we won't add a " "(blank) space in the corresponding position of the second definition.
                def2.add(i , " "); //here I add that blank space, only if the stoplist word in def1 corresponds to a non-stoplist word in def2. Again, we do this so the stoplist word in def1 corresponds to a blank space OR another stoplist word in def2.
                if(mdef == def2.size())
                    mdef++; //In case the shortest definition is the definition to which we just added spaces, we increment mdef++, because that space added increases the length of the shortest definition, and to iterate in this recenlty extended definiton, we have to increment the index with which we iterate.
            }
        } else if (stopList.contains(def2.get(i))) { //this else if does the same than the previous one, but checks for the second definition instead of the first one. And adds blanks to def1 instead of def2 if necessary.
            if (!stopList.contains(def1.get(i))) {
                def1.add(i , " ");
                if(mdef == def1.size())
                    mdef++;
            }
        }
    }
[...]

现在,如果你仔细分析代码,你会发现不会检查最长列表中的所有单词,因为我们使用最短定义的长度作为索引来迭代定义。这很好,不必检查最长定义的剩余单词,它们将对应于另一个定义的空格(如果列表在添加空格后最终没有相同的长度,正如前面的例子所示。)

现在,在解释之后,问题如下:在运行主类(调用包含前面代码的方法)之后,弹出运行时异常:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
        at java.util.ArrayList.rangeCheck(ArrayList.java:571)
        at java.util.ArrayList.get(ArrayList.java:349)
        at main2.main(main2.java:75)

我不明白为什么它发现任何列表为“空”。 我试图以太多方式解决它,我希望我给出了一个很好的解释。

如果我将mdef分配给最长的大小而不是最短的大小,那么这可能有助于提示:

    int mdef = (def1.size() >= def2.size()) ? def1.size() : def2.size();

错误更改为:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 15, Size: 15
    at java.util.ArrayList.rangeCheck(ArrayList.java:571)
    at java.util.ArrayList.get(ArrayList.java:349)
    at asmethods.lcc.turnIntoFlex(lcc.java:55)
    at asmethods.lcc.calLcc(lcc.java:99)
    at main2.main(main2.java:73)' 

其中lcc是包含方法turnIntoFlex的类,其中包含我正在显示的代码段。 “turnIntoFlex”的第55行对应于循环的第一行,即:

    if (stopList.contains(def1.get(i))) { [...]

1 个答案:

答案 0 :(得分:0)

    else if (stopList.contains(def2.get(i))) {
        if (!stopList.contains(def1.get(i))) {
            def1.add(i , " ");
            if(mdef == def2.size())
                mdef++;
        }
    }

if语句mdef == def2.size()不应该是mdef == def1.size()吗?您刚刚添加到def1