Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 遍历列表时出现奇怪的outOfBoundsException_Java_List_Indexoutofboundsexception - Fatal编程技术网

Java 遍历列表时出现奇怪的outOfBoundsException

Java 遍历列表时出现奇怪的outOfBoundsException,java,list,indexoutofboundsexception,Java,List,Indexoutofboundsexception,我正在写一个应用计算语言学许多原理的程序。我现在的问题是下面的代码形成了一个“灵活化两个定义”的方法。 也就是说,它比较了同一个单词的两个不同定义,在每个定义中,空或空格将添加到以后的工作中,并使用修改后的定义(添加空格)。 假设我们有以下两个定义,定义术语“自由落体” 有一个名为“停止列表”的单词列表,其中包含以下单词:“of”、“a”、“in”、“to”和“under”。在此过程之后,定义中包含在“禁止列表”中的每个单词都必须对应于一个空格或其他定义的另一个“禁止列表”单词。 因此,在执行该

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

有一个名为“停止列表”的单词列表,其中包含以下单词:“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.
我为实现这一目标而编写的代码如下:


我不明白为什么它会发现任何列表都是“空的”。 我尝试了太多的方法来解决这个问题,我希望我能给出一个很好的解释

如果我将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是包含TurnToFlex方法的类,该方法包含我正在展示的代码片段。“TurnToFlex”的第55行对应于循环的第一行,即:

if (stopList.contains(def1.get(i))) { [...]
评论: defA1和defA2的值分别是定义。i、 最初,def1和def2是列表,其中每个单独的元素都是一个单词。我无法检查这些列表是否是通过打印来填充的,因为indexoutofboundsexception在循环开始时弹出。但是,我确实打印了mdef、def1.size()和def2.size()的大小值,结果是13或15,这表明在“for”循环开始之前没有列表是空的

mdef++是我最近添加的东西,并不完全是为了解决这个特定的问题,但是在我添加mdef++部分之前,错误就已经出现了。正如我所解释的,目的是在扩展最短列表时(但仅在扩展短列表时)增加mdef+,因此我们迭代短列表中的所有单词,而不是更多的单词。

让我们看看这个:

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中,如果
的话,您的磨合问题在于增量。试试这个:

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.
            mdef=Math.min(def2.size(),def1.size);

        }
    } 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 , " ");
            mdef=Math.min(def2.size(),def1.size);
        }
    }
}
for(int i=0;i
哪些行是73和75?如果def1和def2的大小相同,则mdef的大小将相同。如果您遍历循环并将一个项目添加到其中一个列表中并增加mdef。mdef将大于您的最小列表。无法确定这是否是你跑步时遇到的问题,因为我不知道你的列表中有什么。尝试调试它,看看会发生什么。谢谢@iluxa,你的解决方案非常有意义,但是,当用你编写的代码替换ifs时,问题仍然存在,同样的错误。此外,报告的错误是发现列表为空,这在我看来是最难理解的。很好的假设@user2859911,但是,尽管我相信这很好,问题似乎不是这个。同样的错误也会突然出现。@JoséCasillas您可以提供一个示例停止列表,defA1,defA2,所以我试着自己运行它,更好地了解发生了什么。当然,您可以使用我在解释中给出的两个定义作为示例。语言学家们都知道这个禁止列表,你可以在网上找到它。然而,这包含了大量的词语,我们在本例中感兴趣的词语(对于这两个定义)只有“of”、“a”、“in”、“to”和“under”
if (stopList.contains(def1.get(i))) { [...]
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.
        }
    }
}
if (i < def1.size() && stopList.contains(def1.get(i))) {
  ...
}
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.
            mdef=Math.min(def2.size(),def1.size);

        }
    } 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 , " ");
            mdef=Math.min(def2.size(),def1.size);
        }
    }
}