当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Arrays.binarySearch()方法用法及代码示例


Arrays.binarySearch()是在Java中找到排序数组中元素的最简单,最有效的方法

用法:

public static int binarySearch(data_type arr, data_type key )

其中data_type可以是任何原始数据类型:byte,char,double,int,float,short,long和Object。



描述:
使用二进制搜索算法在给定数据类型的指定数组中搜索指定值。在进行此调用之前,必须对数组进行排序(例如通过Arrays.sort()方法)。如果未排序,则结果不确定。如果数组包含具有指定值的多个元素,则不能保证将找到哪个元素。

参数:
arr-要搜索的数组
key–要搜索的值

返回:
搜索关键字的索引(如果包含在数组中);否则,(-(插入点)-1)。插入点定义为将键插入数组的点:第一个元素的索引大于键,如果数组中的所有元素都小于指定的键,则为a.length。请注意,这保证了当且仅当找到 key 时,返回值才会> = 0。

例子:

Searching for 35 in byteArr[] = {10,20,15,22,35}
will give result as 4 as it is the index of 35

Searching for g in charArr[] = {'g','p','q','c','i'}
will give result as 1 as it is the index of 'g'

Searching for 22 in intArr[] = {10,20,15,22,35};
will give result as 3 as it is the index of 22

Searching for 1.5 in doubleArr[] = {10.2,15.1,2.2,3.5}
will give result as -1 as it is the insertion point of 1.5

Searching for 35.0 in floatArr[] = {10.2f,15.1f,2.2f,3.5f}
will give result as -5 as it is the insertion point of 35.0

Searching for 5 in shortArr[] = {10,20,15,22,35}
will give result as -1 as it is the insertion point of 5

// Java program to demonstrate working of Arrays. 
// binarySearch() 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[] = {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}; 
  
        Arrays.sort(byteArr); 
        Arrays.sort(charArr); 
        Arrays.sort(intArr); 
        Arrays.sort(doubleArr); 
        Arrays.sort(floatArr); 
        Arrays.sort(shortArr); 
  
        byte byteKey = 35; 
        char charKey = 'g'; 
        int intKey = 22; 
        double doubleKey = 1.5; 
        float floatKey = 35; 
        short shortKey = 5; 
  
        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

重要事项:

  • 如果输入列表未排序,则结果不确定。
  • 如果有重复项,则不能保证将找到哪一个。

数组.binarysearch()与Java - Collections.binarySearch()用法及代码示例
Arrays.binarysearch()适用于也可以是原始数据类型的数组。 Collections.binarysearch()适用于ArrayList和LinkedList之类的对象集合。

此方法有多种形式,其中我们还可以指定要搜索的数组范围。我们将在后面的文章中讨论以及在Object数组中进行搜索。




相关用法


注:本文由纯净天空筛选整理自 Arrays.binarySearch() in Java with examples | Set 1。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。