Java中的 Arrays.binarySearch() 示例 | Set 2(在子数组中搜索)
Arrays.binarySearch()| Set 1 介绍了如何在Java中查找已排序数组中的元素。本集将涵盖“如何在给定范围内搜索数组中的键,仅包括起始索引”。
句法 :
public static int binarySearch(data_type[] arr, int fromIndex, int toIndex, data_type key)
参数 :
arr – the array to be searched
fromIndex – the index of the first element (inclusive) to be searched
toIndex – the index of the last element (exclusive) to be searched
key – the value to be searched for
- 它是Java中Java (java.util.Arrays)类中定义的静态内置方法,返回指定键的索引是否在指定范围内。
- 在这里, data_type可以是任何原始数据类型:byte、char、double、int、float、short、long 和 Object。
- 上述函数使用二进制搜索算法在给定数据类型的指定数组范围内搜索指定键。
- 在进行此调用之前,必须对要搜索的指定键的范围进行排序(如通过 Arrays.sort() 方法)。否则,结果将是未定义的。如果指定的数组包含多个与指定键相同的值,则无法保证会找到哪一个。
Returns :
Index of the specified key is found within the specified range in the specified array, Otherwise (-(insertion point) – 1).
插入点定义为将插入指定键的点:范围中大于键的第一个元素的索引,如果范围内的所有元素都小于指定键,则为 toIndex。
注意:这保证了当且仅当找到键时返回值将 >= 0。
例子:
byteArr[] = {10,20,15,22,35}
key = 22 to be searched between the range 2 to 4 in specified array.
Output: 3
charArr[] = {‘g’,’p’,’q’,’c’,’i’}
key = p to be searched between the range 1 to 4 in specified array.
Output: 3
intArr[] = {1,2,3,4,5,6}
key = 3 to be searched between the range 1 to 4 in specified array.
Output: 2
doubleArr[] = {10.2,1.51,2.2,3.5}
key = 1.5 to be searched between the range 1 to 4 in specified array.
Output: -2 as it is the insertion point of 1.5
floatArr[] = {10.2f,15.1f,2.2f,3.5f}
key = 35.0 to be searched between the range 1 to 4 in specified array.
Output: -5
shortArr[] = {10,20,15,22,35}
key = 5 to be searched between the range 0 to 4 in specified array.
Output: -1
执行 :
Java
// Java program to demonstrate working of binarySearch()
// method for specified range in a sorted array.
import java.util.Arrays;
public class GFG {
public static void main(String[] args)
{
byte byteArr[] = { 10, 20, 15, 22, 35 };
char charArr[] = { 'g', 'p', 'q', 'c', 'i' };
int intArr[] = { 1, 2, 3, 4, 5, 6 };
double doubleArr[] = { 10.2, 15.1, 2.2, 3.5 };
float floatArr[] = { 10.2f, 15.1f, 2.2f, 3.5f };
short shortArr[] = { 10, 20, 15, 22, 35 };
Arrays.sort(byteArr);
Arrays.sort(charArr);
Arrays.sort(intArr);
Arrays.sort(doubleArr);
Arrays.sort(floatArr);
Arrays.sort(shortArr);
byte byteKey = 22;
char charKey = 'p';
int intKey = 3;
double doubleKey = 1.5;
float floatKey = 35;
short shortKey = 5;
System.out.println(
byteKey + " found at index = "
+ Arrays.binarySearch(byteArr, 2, 4, byteKey));
System.out.println(
charKey + " found at index = "
+ Arrays.binarySearch(charArr, 1, 4, charKey));
System.out.println(
intKey + " found at index = "
+ Arrays.binarySearch(intArr, 1, 4, intKey));
System.out.println(doubleKey + " found at index = "
+ Arrays.binarySearch(
doubleArr, 1, 4, doubleKey));
System.out.println(floatKey + " found at index = "
+ Arrays.binarySearch(
floatArr, 1, 4, floatKey));
System.out.println(shortKey + " found at index = "
+ Arrays.binarySearch(
shortArr, 0, 4, shortKey));
}
}
22 found at index = 3
p found at index = 3
3 found at index = 2
1.5 found at index = -2
35.0 found at index = -5
5 found at index = -1
例外:
- IllegalArgumentException :当起始索引(fromIndex)大于指定范围的结束索引(toIndex)时抛出。(意味着:fromIndex > toIndex)
- ArrayIndexOutOfBoundsException :如果一个或两个索引无效则抛出,意味着 fromIndex<0 或 toIndex > arr.length。
要点:
- 如果输入列表未排序,则结果未定义。
- 如果有重复,则无法保证会找到哪一个。
参考 :
https://docs.oracle.com/javase/7/docs/api/java Java)