English 中文(简体)
Java的宾搜索和Add
原标题:Binary Search and Add in Java
  • 时间:2011-03-22 21:05:38
  •  标签:
  • java

我将提高搜索方法的效率,我决定使用双轨搜索方法。 我想利用这一搜索手段,在每一项目的字眼清单中增加每个项目编号,并显示每个项目的次数。 但是,我的法典似乎没有发挥作用......

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) 
问题回答

这不是你的唯一问题,但我怀疑,你的双轨搜查没有发现任何东西,因为:

if (min < max) {
         return NOT_FOUND;
 }

非豁免名单总是如此。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签