English 中文(简体)
Java Arrays binarySearch
原标题:Java Arrays binarySearch

这里是我简单的法典,我在那里修建一个强硬阵列,并试图在这个阵列中搜索一个阵列:

String[] arr = new String[5];
arr[0] = "ccc";
arr[1] = "aaa";
arr[2] = "bbb";
arr[3] = "eee";
arr[4] = "ddd";

System.out.println(Arrays.binarySearch(arr,"eee"));

直接摘自Java 6本月文件:

The array must be sorted prior to making this call. If it is not sorted, the results are undefined

实际上,我有数小时的代码,总产出为3,这是在我没有分类的阵列中被收人的地位,但结果似乎并没有像文件所说的那样被“界定”。

我失踪了什么?

最佳回答

"Undefined" means that the algorithm will run on your array, but the result is not guaranteed to be correct (binary search strongly needs a sorted array in order to work). Your example works because this is what happens:

  • enter binary search with first = 0, last = 4, middle = 2 compare
  • array[middle] with "eee" ("bbb"<"eee") => first = 2 + 1; middle = 3;
  • compare array[middle] with "eee" => "found" ; return 3;
问题回答

“未定义”并不意味着“肯定会给你错误的结果”,或“无限期坠毁”。

当我们再谈一下如何实施一部法典时,“未定义”一词意味着方案执行可以做任何事情:

  • Return the wrong answer
  • Loop forever
  • Crash immediately
  • Corrupt some data and cause a crash much later
  • Do something else unintended (e.g. erase your hard drive)
  • Be lucky and return the right answer

作为对方案者的建议,don t 援引未经界定的行为,因为现在或以后,任何事情都会发生、好或坏。


在您的案例中,你测试了“ee”的搜索,并得出正确的结果。

现在试图寻找“ccc”,发生了什么? 寻找“aaa”。 寻找“bbb”。 寻找“ddd” 我很幸运的是,其中一些搜查将回到“没有发现”的状态,尽管其价值在阵列中明显。

你们没有知道,“结果没有界定”,包括像本案那样可能作出“正确”回答。

如果你把rr升至“ee”,就会看到不同的结果。

许多机构、书籍、教授......等界定的普通搜索 要求以字母或数字方式对各项要素进行分类。

import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    String[] arr = new String[6];
    arr[0] = "ccc";
    arr[1] = "aaa";
    arr[2] = "bbb";
    arr[3] = "eee";
    arr[4] = "ddd";
    arr[5] = "aaa";
    System.out.println(Arrays.toString(arr));
    System.out.println(""eee" was found at index: " + Arrays.binarySearch(arr, "eee"));
    Arrays.sort(arr);
    System.out.println(Arrays.toString(arr));
    System.out.println(""eee" was found at index: " + Arrays.binarySearch(arr, "eee"));
  }
}

除了ese回答外,本文还修改了你收回错误答案的方案:

public class Main {
    public static void main(String[] args) {
        String[] arr = new String[6];
        arr[0] = "ccc";
        arr[1] = "aaa";
        arr[2] = "bbb";
        arr[3] = "eee";
        arr[4] = "ddd";
        arr[5] = "aaa";

        System.out.println(Arrays.binarySearch(arr, "eee"));
    }
}




相关问题
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 ...

热门标签