binarysearch java_关于java的binarySearch()方法

展开全部

概述

binarysearch为在指定数组中查找指定值得索引值e68a8462616964757a686964616f31333365656632,该值在范围内找得到则返回该值的索引值,找不到则返回该值的插入位置,如果该值大于指定范围最大值则返回-(maxlength+1),而:

int w=Arrays.binarySearch(a,1,5,8);  查找的范围为索引值1-5,:2,3,4,5,6

8并不在此范围中,且8大于最大索引值的6,所以返回-(5+1):-6

解析

查看java源码,可以看到,binarySearch()方法是重载方法,提供了两种形参方式:

小贴士:binarySearch()方式内部实现用的是二分法查找,所以在查找前需要将数组进行排序,且数组中不能出现相同元素,否则查找出来的索引会不清楚是哪一个的:

1)默认范围(数组长度)查找指定值索引:

格式:binarySearch(object[ ], object key);

如果key在数组中,则返回搜索值的索引;否则返回-1(key小于数组中的任意一个元素)或者”-“(插入点)。插入点是索引键将要插入数组的那一点,即第一个大于该键的元素索引。

key的值在数组范围内则索引从0开始计数;

key值不存在数组范围内(大于数组最小元素)则从1开始计数;

实例:import java.util.Arrays; public class test {    public static void main(String[] args)    {        int[]b=new int[]{4,25,10,95,06,21};        System.out.println("原数组为:");        for(int dim1:b)        {            System.out.print(""+dim1+" ");        }        Arrays.sort(b);        System.out.println("排序后为:");        for(int x:b)        {            System.out.print(x+" ");        }        System.out.println();        int index=Arrays.binarySearch(b, 2);        System.out.println("关键字2的返回值为:"+index);                 index=Arrays.binarySearch(b, 20);        System.out.println("关键字20的返回值为:"+index);                 index=Arrays.binarySearch(b, 30);        System.out.println("关键字30的返回值为:"+index);                 index=Arrays.binarySearch(b, 100);        System.out.println("关键字100的返回值为:"+index);                 index=Arrays.binarySearch(b, 10);        System.out.println("关键字10的返回值为:"+index);    }}

//out:

2c6dfb4e8143f843896fb77c71bbeab5.png

2)指定范围查找指定元素的索引:

格式:binarySearch(object[ ], int fromIndex, int endIndex, object key);

注意点:

1、该搜索键在范围内,但不在数组中,由1开始计数;

2、该搜索键在范围内,且在数组中,由0开始计数;

3、该搜索键不在范围内,且小于范围内元素,由1开始计数;

4、该搜索键不在范围内,且大于范围内元素,返回-(endIndex + 1);(特列)

实例:import java.util.Arrays; public class test {    public static void main(String[] args)    {        int[]b=new int[]{4,25,10,95,06,21};        System.out.println("原数组为:");        for(int dim1:b)        {            System.out.print(""+dim1+" ");        }        Arrays.sort(b);        System.out.println("排序后为:");        for(int x:b)        {            System.out.print(x+" ");        }        System.out.println();        int index=Arrays.binarySearch(b,1,3,2);        System.out.println("关键字2的返回值为:"+index);                 index=Arrays.binarySearch(b,1,3,20);        System.out.println("关键字20的返回值为:"+index);                 index=Arrays.binarySearch(b,1,4,30);        System.out.println("关键字30的返回值为:"+index);                 index=Arrays.binarySearch(b,1,4,100);        System.out.println("关键字100的返回值为:"+index);                 index=Arrays.binarySearch(b,1,4,10);        System.out.println("关键字10的返回值为:"+index);    }}

//out:

08efd761c52a83f3f60ad8b51fb16153.png

拓展内容

array数组详解

9d9d5f1511169490652d16d0542901ed.png

数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同。

Java 语言中提供的数组是用来存储固定大小的同类型元素。

你可以声明一个数组变量,如 numbers[100] 来代替直接声明 100 个独立变量 number0,number1,....,number99。

本教程将为大家介绍 Java 数组的声明、创建和初始化,并给出其对应的代码。

Arrays 类

java.util.Arrays 类能方便地操作数组,它提供的所有方法都是静态的。

具有以下功能:给数组赋值:通过 fill 方法。

对数组排序:通过 sort 方法,按升序。

比较数组:通过 equals 方法比较数组中元素值是否相等。

查找数组元素:通过 binarySearch 方法能对排序好的数组进行二分查找法操作。

1            public static int binarySearch(Object[] a, Object key)

用二分查找算法在给定数组中搜索给定值的对象(Byte,Int,double等)。数组在调用前必须排序好的。如果查找值包含在数组中,则返回搜索键的索引;否则返回 (-(插入点) - 1)。

2            public static boolean equals(long[] a, long[] a2)

如果两个指定的 long 型数组彼此相等,则返回 true。如果两个数组包含相同数量的元素,并且两个数组中的所有相应元素对都是相等的,则认为这两个数组是相等的。换句话说,如果两个数组以相同顺序包含相同的元素,则两个数组是相等的。同样的方法适用于所有的其他基本数据类型(Byte,short,Int等)。

3            public static void fill(int[] a, int val)

将指定的 int 值分配给指定 int 型数组指定范围中的每个元素。同样的方法适用于所有的其他基本数据类型(Byte,short,Int等)。

4            public static void sort(Object[] a)

对指定对象数组根据其元素的自然顺序进行升序排列。同样的方法适用于所有的其他基本数据类型(Byte,short,Int等)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值