Java 自定义拼写检查生成错误

Java 自定义拼写检查生成错误,java,indexoutofboundsexception,spell-checking,Java,Indexoutofboundsexception,Spell Checking,我正在加载一个包含近80000个单词的文件。它将被用作主要的拼写检查词典。单词的顺序已经随机化了。我正在加载的另一个文件中有拼写错误的单词,我必须检查。它还为拼写错误的单词提供建议 public void spellCheckDocument(ArrayList<String> dictionary){ long startCheck = System.currentTimeMillis(); for(String words: collectionO

我正在加载一个包含近80000个单词的文件。它将被用作主要的拼写检查词典。单词的顺序已经随机化了。我正在加载的另一个文件中有拼写错误的单词,我必须检查。它还为拼写错误的单词提供建议

public void spellCheckDocument(ArrayList<String> dictionary){
        long startCheck = System.currentTimeMillis();
        for(String words: collectionOfParagraphs)
            for(String word: words.split("[^a-zA-Z_0-9']+")){
                int index = Collections.binarySearch(dictionary, word.toLowerCase());
                if(index<0 && word.length()>0){

                    //collectionOfMisspelledWord.add(word+" Possible correct word: "+dictionary.get(-index+1)+" "+dictionary.get(-index)+" "+dictionary.get(-index-1));
                    //System.out.printf("%s Misspelled, possible correct words: %s, %s, %s\n", word, dictionary.get(-index+1),dictionary.get(-index),dictionary.get(-index-1));
                    possibleCorrectSpellings = new Document(word, dictionary.get(-index+1),dictionary.get(-index), dictionary.get(-index-1));
                    collectionOfMisspelledWord.add(possibleCorrectSpellings);
                }           
        }

--------error----------
java.lang.IndexOutOfBoundsException: Index: 380, Size: 379
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at file.Document.spellCheckDocument(Document.java:82)
公共无效拼写检查文档(ArrayList字典){
long startCheck=System.currentTimeMillis();
for(字符串字:collectionOfParagraphs)
for(字符串字:words.split(“[^a-zA-Z_0-9']+”)){
int index=Collections.binarySearch(字典,word.toLowerCase());
if(index0){
//collectionOfMisspelledWord.add(word+“可能正确的单词:”+dictionary.get(-index+1)+“+dictionary.get(-index)++”“+dictionary.get(-index-1));
//System.out.printf(“%s拼写错误,可能正确的单词:%s,%s,%s\n”,单词,dictionary.get(-index+1),dictionary.get(-index),dictionary.get(-index-1));

possibleCorrectSpellings=新文档(word,dictionary.get(-index+1),dictionary.get(-index),dictionary.get(-index-1)); collectionOfMisspelledWord.add(可能拼写正确); } } --------错误---------- java.lang.IndexOutOfBoundsException:索引:380,大小:379 位于java.util.ArrayList.rangeCheck(ArrayList.java:653) 获取(ArrayList.java:429) 位于file.Document.spellCheckDocument(Document.java:82)
来自以下文件:

否则,
((插入点)-1)
。插入点定义为将键插入列表的点:第一个元素的索引大于键,或者如果列表中的所有元素都小于指定键,则定义为list.size()


这意味着您有时会得到一个超过列表中最后一个元素的索引。您需要为这种情况添加特殊处理(这可能意味着您不知道哪些单词可能是正确的)。

ssibleCorrectionSpellings=new Document(word,dictionary.get(-index+1),dictionary.get(-index),dictionary.get(-index),dictionary.get(-index-1));