0

HTML ファイルの単語インデクサーの実装を書いています。コンストラクターに問題があります。

コンストラクターでは、HTML ファイル内の各単語をスキャンし、それを TreeMap に追加します)。ここで、リンクされたリストは、キー (単語) がファイル内に現れるインデックスのコレクションです。テスト中、連結リストが 1 より大きくなることはありません。誰かが私のコードを見て、物事が混乱している正しい方向に私を向けることができるかどうか疑問に思っています.

while(scanner.hasNext()) {
            String temp = scanner.next().toLowerCase();

            if(this.wordMap.containsKey(temp)) {
                LinkedList<Integer> tempList = this.wordMap.get(temp);
                tempList.add(currentIndex);

                wordMap.put(temp, tempList);

                wordCount++;
                currentIndex++;
            }
            else {
                LinkedList<Integer> tempList = new LinkedList<Integer>();
                tempList.add(currentIndex);
                wordMap.put(temp, tempList);

                wordCount++;
                currentIndex++;
            }
        }
4

1 に答える 1

0

これは答えではありません、コードで自分自身を繰り返さないでください(DRY):

    while(scanner.hasNext()) {
        String temp = scanner.next().toLowerCase();

        LinkedList<Integer> tempList;
        if(this.wordMap.containsKey(temp)) {
            tempList = this.wordMap.get(temp);
        } else {
            tempList = new LinkedList<Integer>();
        }

        tempList.add(currentIndex);
        wordMap.put(temp, tempList);
        wordCount++;
        currentIndex++;
    }

コードにバグが見つかりません。スキャナーで区切り文字を確認することをお勧めします。

于 2012-11-04T09:39:59.953 に答える