Java Arrays.binarySearch()方法与实例 | Set 1
Arrays.binarySearch() 方法使用二进制搜索算法在指定数据类型的数组中搜索指定的值。在调用此方法之前,数组必须通过Arrays.sort()方法进行排序。如果它没有被排序,那么结果是未定义的。如果数组中包含多个具有指定值的元素,不能保证哪一个会被找到。让我们滑过下面提供的插图,如下所示。
插图
在byteArr[] = {10,20,15,22,35}中搜索35,结果是4,因为是35的索引。
将得到4的结果,因为它是35的索引
在charArr[] = {‘g’,’p’,’q’,’c’,’i’}中搜索g。
将得到0的结果,因为它是’g’的索引
在intArr[] = {10,20,15,22,35}中搜索22。
将会得到3的结果,因为它是22的索引
在 doubleArr[] = {10.2,15.1,2.2,3.5}中搜索1.5。
将会得到-1的结果,因为它是1.5的插入点
在floatArr[] = {10.2f,15.1f,2.2f,3.5f}中搜索35.0,结果为-5,因为是1.5的插入点。
结果是-5,因为它是35.0的插入点。
在shortArr[] = {10,20,15,22,35}中搜索5,结果是-1,因为是35.0的插入点。
结果将是-1,因为它是5的插入点。
它是在Java中寻找一个排序数组中的元素的最简单和最有效的方法。
语法
public static int binarySearch(data_type arr, data_type key)
记住: 这里的数据类型可以是任何一种原始数据类型,如byte, char, double, int, float, short, long, 甚至object也可以。
参数
- 要搜索的数组
- 要搜索的值
返回类型: 搜索键的索引,如果它包含在数组中;否则,(-(插入点)-1)。插入点被定义为钥匙插入数组的点:第一个大于钥匙的元素的索引,如果数组中的所有元素都小于指定的钥匙,则为a.length。请注意,这保证了当且仅当键被找到时,返回值将>=0。
有一些重要的地方需要注意,如下。
- 如果输入的列表没有被排序,那么结果是不确定的。
- 如果有重复的,不能保证哪一个会被找到。
如上所述,我们已经讨论过,我们可以在 Arrays.binarysearch()和 Collections.binarysearch( )中操作这个算法 。 Arrays.binarysearch()适用于数组,也可以是原始数据类型。Collections.binarysearch()则适用于对象集合,如ArrayList和LinkedList。
例子1 :
// Java program to demonstrate working of Arrays.
// binarySearch() in a sorted array
// Importing Arrays class from
// java.util package
import java.util.Arrays;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing byte arrays
// to search over them
byte byteArr[] = { 10, 20, 15, 22, 35 };
char charArr[] = { 'g', 'p', 'q', 'c', 'i' };
int intArr[] = { 10, 20, 15, 22, 35 };
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 };
// Using sort() method of Arrays class
// and passing arrays to be sorted as in arguments
Arrays.sort(byteArr);
Arrays.sort(charArr);
Arrays.sort(intArr);
Arrays.sort(doubleArr);
Arrays.sort(floatArr);
Arrays.sort(shortArr);
// Primitive datatypes
byte byteKey = 35;
char charKey = 'g';
int intKey = 22;
double doubleKey = 1.5;
float floatKey = 35;
short shortKey = 5;
// Now in sorted array we will fetch and
// return elements/indiciesaccessing indexes to show
// array is really sorted
// Print commands where we are implementing
System.out.println(
byteKey + " found at index = "
+ Arrays.binarySearch(byteArr, byteKey));
System.out.println(
charKey + " found at index = "
+ Arrays.binarySearch(charArr, charKey));
System.out.println(
intKey + " found at index = "
+ Arrays.binarySearch(intArr, intKey));
System.out.println(
doubleKey + " found at index = "
+ Arrays.binarySearch(doubleArr, doubleKey));
System.out.println(
floatKey + " found at index = "
+ Arrays.binarySearch(floatArr, floatKey));
System.out.println(
shortKey + " found at index = "
+ Arrays.binarySearch(shortArr, shortKey));
}
}
输出
35 found at index = 4
g found at index = 1
22 found at index = 3
1.5 found at index = -1
35.0 found at index = -5
5 found at index = -1
这个方法有一些变种,我们也可以指定数组的范围来进行搜索。我们将在以后的文章中讨论这个问题,以及在对象数组中搜索。
例2 :
// Java Program to Illustrate binarySearch() method
// of Collections class
// Importing required classes
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating empty List
List<Integer> al = new ArrayList<Integer>();
// Adding elements to the List
al.add(12);
al.add(53);
al.add(23);
al.add(46);
al.add(54);
// Using binarySearch() method of Collections class
// over random inserted element and storing the
// index
int index = Collections.binarySearch(al, 23);
// Print and display the index
System.out.print(index);
}
}
输出
2