2013-10-22 35 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

是的泰勒你是對的。真正的代碼是正確的,不用擔心。 –

相關問題