首页 文章

使用二进制搜索查找数组中的元素

提问于
浏览
1

尝试使用 Arrays.binarySearch() 在数组中搜索字符串并返回索引 . 但是每次我调用 Arrays.binarySearch() 时都会出现以下异常 -

Exception in thread "main" java.lang.NullPointerException
at java.util.Arrays.binarySearch0(Unknown Source)
at java.util.Arrays.binarySearch(Unknown Source)
at project.ArrayDirectory.lookupNumber(ArrayDirectory.java:97)
at project.test.main(test.java:12)

这是我的 ArrayDirectory class -

public class ArrayDirectory implements Directory {

static Entry[] directory = new Entry[50];

@Override
public void addEntry(String surname, String initials, int extension) {

    int n = 0;
    for (int i = 0; i < directory.length; i++) { // counting number of
                                                    // entries in array
        if (directory[i] != null) {
            n++;
        }

    }

    if (n == directory.length) {
        Entry[] temp = new Entry[directory.length * 2]; // if array is full
                                                        // double the
                                                        // length.

        for (int i = 0; i < directory.length; i++)
            temp[i] = directory[i];
        directory = temp;
    }

    int position = -1;
    for (int i = 0; i < directory.length; i++) {

        position = i;

        if (directory[i] != null) { // sorting the array into alphabetical
                                    // order by surname.

            int y = directory[i].getSurname().compareTo(surname);
            if (y > 0) {
                break;
            }
        }

        else if (directory[i] == null) {
            break;
        }

    }

    System.arraycopy(directory, position, directory, position + 1,
            directory.length - position - 1);

    directory[position] = new Entry(initials, surname, extension); // placing
                                                                    // new
                                                                    // entry
                                                                    // in
                                                                    // correct
                                                                    // position.

}

@Override
public int lookupNumber(String surname, String initials) {
    // TODO Auto-generated method stub

    Entry lookup = new Entry(surname, initials);
    int index = Arrays.binarySearch(directory, lookup);
    return index;
}

任何想法我如何使用二进制搜索来找到正确的索引?

谢谢你的帮助 .

编辑 -

我在 Entry 课程中也覆盖了 comapreTo -

public int compareTo(Entry other) {
    return this.surname.compareTo(other.getSurname());
}

3 回答

  • 0

    在你的调用

    int index = Arrays.binarySearch(directory,lookup);
    

    directory 似乎只包含 null 元素 . 检查您是否正确初始化元素 .

  • 0

    我注意到两件事:

    static Entry [] directory = new Entry [1];
    

    首先,该代码为数组中的一个Entry分配空间 . 它实际上并不实例化条目 . 也就是说, directory[0]null . 其次,对具有一个条目的数组进行二进制搜索是疯狂的 . 只有一个元素 . 它必须是 directory[0] . 最后,您应该对数组进行排序以对其进行二进制搜索 .

  • 0

    二进制搜索背后的基本概念是以下步骤的递归(注意,搜索假定元素的列表或数组以某种形式排序,并且元素存在于那里 . ):

    • 转到数组的中间元素 .

    • 检查搜索到的元素是否等于中间的元素 . 如果是,则返回其索引 .

    • 如果没有,则检查搜索到的元素是否比中间的元素“更小”或“更大” .

    • 如果它较小,则仅使用数组的下半部/前半部分而不是整体进入步骤1 .

    • 否则仅使用数组的上半部分或后半部分而不是整数进入第1步 .

    当阵列连续分为2时,它最终将达到1的大小,从而得到结果 .

    现在,假设您正在寻找int数组中的整数 . 这是代码的样子:

    public static int binarySearch(int number, int[] array)
    {
        boolean isHere = false;
        Integer index =0;
    
        for(int i=0;i<array.length;i++)
        {
            if(array[i] == number)
            {
                isHere = true;
                i = array.length;
            }
        }
        if(!isHere)
        {
            index = -1;
        }
        else
        {
            int arrayStart = 0;
            int arrayEnd = array.length;
             index = binarySearch(number, arrayStart, arrayEnd, array);
        }
    
        return index;
    }
    
    
    private static int binarySearch(int number, int start, int end, int[] array)
    {
        // this formula ensures the index number will be preserved even if 
               // the array is divided later.
        int middle = (start+ end)/ 2;
    
        if(array[middle] == number)
        {
            return middle;
        }
        else
        {
            if(number < array[middle])
            {
                //searches the first half of the array
            return binarySearch(number, start, middle, array);
            }
            else
            {
                // searches the last half of the array
                return binarySearch(number, middle, end, array);
            }
        }
    }
    

    您可以在示例中使用compareTo()方法而不是<,>,&==运算符 . 逻辑应该仍然是相同的 .

相关问题