首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >迭代列表时的问题: Java中的IndexOutOfBoundsException

迭代列表时的问题: Java中的IndexOutOfBoundsException
EN

Stack Overflow用户
提问于 2013-10-27 12:37:28
回答 2查看 256关注 0票数 1

我正在写一个应用了许多计算语言学原理的程序。我现在的问题是下面这段代码形成了一个“柔性化两个定义”的方法。这是,它比较了同一个词的两个不同的定义,并在每个定义中添加空格或空格,以便稍后使用更改后的定义(添加空格)。假设我们有以下两个定义,定义了术语“自由落体”。

代码语言:javascript
复制
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

有一个称为非索引字列表的单词列表,其中包含单词:" of“、"a”、"in“、"to”和"under“。在该过程之后,定义中也包含在非索引字表中的每个单词必须对应于空格或另一个定义的非索引字表中的另一个单词。因此,在执行这样的过程之后,前面的定义在两个不同的列表中表示,应该如下所示:

代码语言:javascript
复制
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.

我为实现这一点而编写的代码如下:

代码语言:javascript
复制
[...]

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++;
        }
    }
}

[...]

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

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

代码语言:javascript
复制
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赋给最长的大小而不是最短的大小,这可能会有所帮助,即:

代码语言:javascript
复制
int mdef = (def1.size() >= def2.size()) ? def1.size() : def2.size();

错误更改为:

代码语言:javascript
复制
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行对应于循环的第一行,即:

代码语言:javascript
复制
if (stopList.contains(def1.get(i))) { [...]

备注: defA1和defA2的值分别是定义。即def1和def2,最初是列表,其中每个单独的元素是一个单词。我不能检查是否通过打印这些列表来填充它们,因为indexoutofboundsexception异常在循环开始的那一刻弹出。但是,我输出了mdef、def1.size()和def2.size()的大小的值,结果这些值变成了13或15,这表明在"for“循环开始之前没有列表是空的。

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

EN

回答 2

Stack Overflow用户

发布于 2013-10-27 13:58:32

伙计,我想我搞定了。我修改了代码,但我希望您能理解我所做的:

代码语言:javascript
复制
static public void main(String[] argv) {
    String[] sList = "of a in to under".split(" ");
    String[] definition1 = "Free fall descent of a body subjected only to the action of gravity"
            .split(" ");
    String[] definition2 = "Free fall movement of a body in a gravitational field under the influence of gravity"
            .split(" ");
    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);
    }
    for (String word : definition2) {
        def2.add(word);
    }
    for (String word : sList) {
        stopList.add(word);
    }

    int mdef = (def1.size() <= def2.size()) ? def1.size() : def2.size(); // Shortest
                                                                            // length

    for (int i = 0; i < mdef; i++) {
        System.out.println(i);
        if (!stopList.contains(def1.get(i)) && !stopList.contains(def2.get(i))) {
            continue;
        }

        else if (stopList.contains(def1.get(i)) && stopList.contains(def2.get(i))) {
            continue;
        }

        else if (!stopList.contains(def1.get(i)) && stopList.contains(def2.get(i))) {
            def1.add(i, " ");
            mdef = (def1.size() <= def2.size()) ? def1.size() : def2.size(); // define mdef again
        }

        else if (stopList.contains(def1.get(i)) && !stopList.contains(def2.get(i))) {
            def2.add(i, " ");
            mdef = (def1.size() <= def2.size()) ? def1.size() : def2.size(); // define mdef again
        }

    }

    for (String word : def1) {

        if (word.equals(" "))
            System.out.print("_ ");
        else
            System.out.print(word+" ");
    }

    System.out.println();

    for (String word : def2) {
        if (word.equals(" "))
            System.out.print("_ ");
        else
            System.out.print(word+" ");
    }           
}
票数 0
EN

Stack Overflow用户

发布于 2013-10-27 13:43:04

这就是你正在使用的代码吗?我刚刚运行了它,它工作得很好,我使用:

代码语言:javascript
复制
import java.util.*;

public class HelloWorld {

    public static void main(String []args) {
        String stoplist= "of a in to and under";
        String defA1 = "Free fall descent  of a body subjected only to            the   action of  gravity";
        String defA2 = "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++;
                }
            }
        }

        for (String word : def1) {
            System.out.print(word+",");
        }

        System.out.println();

        for (String word : def2) {
            System.out.print(word+",");
        }
    }
}
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19614669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档