1

以下のクラスは、単語の位置を行ごとにインデックス化するためのものです。エラーをスローしているメソッドは、現在のインデックスに別のドキュメントのインデックスを追加することを意図しています。つまり、最初の文書に 6 行ある場合、追加された文書の最初の行は 7 行として索引付けする必要があります。

public class DocumentIndex {
  // a NavigableMap implementation to store indexed words and their locations
  private TreeMap<String, ArrayList<Integer>> map = new TreeMap<String, ArrayList<Integer>>();

    /**
     * A method to append another index onto the main index
     * @param       indexAppendix       the additional index to be appended onto the main index
     */
  public void append(DocumentIndex indexAppendix){
    if(indexAppendix == null){
        throw new NullPointerException();
    }
    Integer docEnd = 0;                                 // the last line recorded in the main index
    Set<String> set = map.keySet();                     // a Set of the key values from map
    //select each key
    for(Iterator<String> iter = set.iterator(); iter.hasNext();){
        String key = iter.next();                       // the current key value
        // for each key select contents and determine the highest value
        for(Iterator<Integer> iter2 = this.find(key).iterator(); iter2.hasNext();){
            Integer compare = iter2.next();             // the key index current value
            if(compare>docEnd){
                docEnd=compare;
            }
        }
    }

    // for each key find an index value
    for(Iterator<String> iter = set.iterator(); iter.hasNext();){
        String key = iter.next();                       // the current key value
        // for each index value map that value adjusting for the length of the original document
        ArrayList<Integer> toAdd = new ArrayList<Integer>();
        for(Iterator<Integer> iter2 = this.find(key).iterator(); iter2.hasNext();){
            Integer addIter = iter2.next();
            toAdd.add(addIter); // the current index value
        }

        /**
         *Below is the loop in which the error is thrown
         */
        for(Iterator<Integer> iter3 = toAdd.iterator(); iter.hasNext();){

            Integer addIter = iter3.next();      // The error is thrown on this line

            map.get(key).add(addIter+docEnd);
        }
    }
}

私は何を間違っていますか?

4

2 に答える 2

4

ルイス・ワッサーマンはそれを釘付けにしました。

「新しい」Java ループ構文を使用していればfor、コードははるかに単純になり、そもそもその間違いを犯すことはありませんでした (できませんでした!!)。たとえば、「新しい」for構文を使用すると、コードは大まかに次のようになります。

    ...
    Integer docEnd = 0;
    Set<String> set = map.keySet();  
    for (String key : set) {
         for (Integer compare : this.find(key)) {
             if (compare < docEnd){
                  docEnd = compare;
             }
         }
    }

    for (String key : set) {
        ArrayList<Integer> toAdd = new ArrayList<Integer>();
        for (String add : this.find(key)) {
            toAdd.add(add); 
        }
        for (Integer add : toAdd) {
            map.get(key).add(add * docEnd);
        }
    } 

そのほうがよほど読みやすいのではないか。


この構文は 2004 年にリリースされた Java 5 で導入されたため、「新しい」を引用符で囲みました。これは、すべての実践 Java プログラマーおよびインストラクターの標準レポートの一部である必要があります。

上記のコードをコピーして貼り付けないでください。私の要点を説明することのみを目的としています。

于 2012-04-21T01:15:57.697 に答える
3

問題のあるループのループ条件は、iter3.hasNext()ではなく、である必要がありiter.hasNext()ます。

于 2012-04-21T00:41:30.943 に答える