2013-10-22 33 views
0

我正在编写代码以制作一个应用计算语言学许多原理的程序。我现在的问题是下面的一段代码形成一个“灵活两个定义”的方法。 这是比较同一单词的两个不同定义,并且在每个定义中,空白或空白空间将被添加到稍后用改变的定义(添加空白空间)工作。
假设我们有以下两个定义,定义术语“自由落体”。在Java中迭代列表时遇到严重问题[indexOutOfBounds]

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 

有一个称为终止列表单词的列表,其中包含的话:“中”,“一”,“中”,“来”和“下”。在该过程之后,定义中也包含在停止列表中的每个单词必须对应于另一个定义的空白空间或另一个停止列表字。 所以执行这样的过程后,以前的定义,在两个不同的清单表示的,应该是这样的:

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++; 
      } 
     } 
    } 
[...] 

现在,如果仔细分析代码,您会意识到并不是所有最长列表的单词都会被检查,因为我们使用th的长度迭代了定义最短的定义为索引。这很好,最长定义的剩余单词不必检查,它们将与另一个定义的空格相对应(如果列表在最后加入空格后最终不具有相同的长度,如前面的例子所示)。现在

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

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

哪条线是75线? – rgettman

+0

我想它与增加循环范围的mdef ++有关,导致索引超出较短列表的范围 – Evans

+0

而不是mdef =(def1.size()<= def2.size() )? def1.size():def2.size(),你可以使用更简单的Math.min(def1.size(),def2)。尺寸()); – Akkusativobjekt

回答

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

+0

是的泰勒你是对的。真正的代码是正确的,不用担心。 –

相关问题