我将提高搜索方法的效率,我决定使用双轨搜索方法。 我想利用这一搜索手段,在每一项目的字眼清单中增加每个项目编号,并显示每个项目的次数。 但是,我的法典似乎没有发挥作用......
private static void searchAndAdd(List<Word> wl, String w, int ln) {
boolean found = false;
Iterator<Word> wit = wl.listIterator();
int min = 0;
int max = wl.size()-1;
int bsearch = binarySearch(wl, w, min, max);
while (found && wit.hasNext()) {
Word wd = wit.next();
if (bsearch == -1 )
{
wd.addLineNumber(ln);
}
}
if (!found)
wl.add(0, new Word(w, ln));
}
private static int binarySearch(List<Word> arr, String w, int min, int max) {
if (min < max) {
return NOT_FOUND;
}
int mid = (min + max) /2;
Word w2 = arr.get(mid);
if (w.compareTo(w2.toString()) > 0) {
return binarySearch(arr, w, mid + 1, min);
} else if (w.compareTo(w2.toString()) < 0) {
return binarySearch(arr, w, min, mid - 1);
} else {
return mid;
}
我总是发现这一错误:
in thread "main" java.lang.NullPointerException
at Concordance.binarySearch(Concordance.java:131)
at Concordance.searchAndAdd(Concordance.java:114)
at Concordance.main(Concordance.java:51)