【问题标题】:Generic Binary Search Fails Using A Key Of Consecutive Characters使用连续字符的键进行通用二进制搜索失败
【发布时间】:2015-07-13 22:53:33
【问题描述】:

我有一个通用的二进制搜索,它似乎与Integers 一起运行良好。但是,当我尝试将它与Strings 一起使用时,它有时会崩溃,并在各个行显示ArrayIndexOutOfBoundsException;特别是我多次使用同一个字母指定一个单词的键。例如String key = "peach"; 返回正确的索引,String key = "blueberry"; 未找到,String key = "scoop"; 导致失败。它似乎与T 中的(key.equals(list[mid])) 有关,但我无法理解。感谢您的帮助。

public class BinarySearch {

private BinarySearch() { }

private static <T extends Comparable<? super T>> int search(T[] list, int first, int last, T key){
    int foundPosition;
    int mid = first + (last - first) / 2;  
    if (first > last)
        foundPosition = -1;
    else if (key.equals(list[mid]))
        foundPosition = mid;
    else if (key.compareTo(list[mid]) < 0)
        foundPosition = search(list, first, mid - 1, key);
    else
        foundPosition = search(list, mid + 1, last, key);

    return foundPosition;
} 

public static void main(String args[]) {

Integer [] a = {0,2,4,6,8,10,12,14,16};
int finalIndex = 9;
System.out.println("Integer test array contains...");
    for (Integer a1 : a) {
        System.out.print(a1 + " ");
    }
int result;
for (int key = -4; key < 11; key++) {
    result = BinarySearch.search(a, 0, finalIndex, key);
    if (result < 0)
        System.out.println("\n" + key + " is not in the array.");
    else
        System.out.println("\n" + key + " is at index " + result + ".");
}

String[] searchFruits = {"lemon", "apple", "banana", "peach", "pineapple", "grapes", "blueberry", "papaya"};   
System.out.println("\nChecking fruits...");
System.out.println("String test array contains...");
 for (String a1 : searchFruits) {
        System.out.print(a1 + " ");
    }
int fruit = 8;
int fresult;
String key = "blueberry";
    fresult = BinarySearch.search(searchFruits, 0, fruit, key);
    if (fresult < 0)
        System.out.println("\n" + key + " is not in the array.");
    else
        System.out.println("\n" + key + " is at index " + fresult + ".");
}

}

【问题讨论】:

  • 仅供参考,您始终可以使用调试器单步执行代码以查看发生了什么。

标签: java arrays generics binary-search compareto


【解决方案1】:

所以问题是您使用数组的大小作为索引,导致数组索引越界问题。

具体

int finalIndex = 9;

int fruit = 8;

现在您有时只看到异常的原因取决于二进制搜索的方式。如果您要搜索的值小于中间值,它将沿着索引的下半部分向下工作,从而达到零,并且永远不会抛出索引越界异常。但是,如果该值大于中间值,则您将递增,直到您达到最后一个值,在本例中为“8”,这将使索引超出范围。

您需要将索引值减一以考虑从 0 开始的索引。

例子:

int finalIndex = a.length-1;

int fruit = searchFruits.length-1;

【讨论】:

  • 好尴尬。 +1为了解释和温柔哈哈。谢谢,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-22
  • 1970-01-01
  • 2021-04-15
  • 2017-04-30
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多