java.util.Arrays.binarySearch方法

java.util.Arrays.binarySearch方法

Modifier and Type 方法 描述
static int binarySearch(byte[] a, byte key) 使用二进制搜索算法搜索指定值的指定字节数组。
static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key) 使用二进制搜索算法搜索指定值的指定字节数组的范围。
static int binarySearch(char[] a, char key) 使用二进制搜索算法搜索指定数组的指定值。
static int binarySearch(char[] a, int fromIndex, int toIndex, char key) 使用二分搜索算法搜索指定值的指定数组的范围。
static int binarySearch(double[] a, double key) 使用二进制搜索算法搜索指定值的指定数组的双精度值。
static int binarySearch(double[] a, int fromIndex, int toIndex, double key) 使用二分搜索算法搜索指定值的指定数组的双精度范围。
static int binarySearch(float[] a, float key) 使用二叉搜索算法搜索指定数组的浮点数。
static int binarySearch(float[] a, int fromIndex, int toIndex, float key) 使用二分搜索算法搜索指定数组的浮点数范围。
static int binarySearch(int[] a, int key) 使用二叉搜索算法搜索指定的int数组的指定值。
static int binarySearch(int[] a, int fromIndex, int toIndex, int key) 使用二叉搜索算法搜索指定值的指定数组的范围。
static int binarySearch(long[] a, int fromIndex, int toIndex, long key) 使用二分搜索算法搜索指定值的指定数组的范围。
static int binarySearch(long[] a, long key) 使用二进制搜索算法搜索指定数组的指定数组。
static int binarySearch(short[] a, int fromIndex, int toIndex, short key) 使用二进制搜索算法搜索指定值的指定数组的短整型范围。
static int binarySearch(short[] a, short key) 使用二进制搜索算法搜索指定值的指定数组的指定值。
static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key) 使用二进制搜索算法搜索指定对象的指定数组的范围。
static int binarySearch(Object[] a, Object key) 使用二叉搜索算法搜索指定对象的指定数组。
static int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator c) 使用二进制搜索算法搜索指定对象的指定数组的范围。
static int binarySearch(T[] a, T key, Comparator c) 使用二叉搜索算法搜索指定对象的指定数组。

binarySearch(int[] a, int key)

import java.util.Arrays;

public class TestBinarySearch {
    public static void main(String[] args) {
        test();

    }
    //测试  binarySearch()方法
    public static void test(){
        int[] a = {1,2,3,5,10,8};
        Arrays.sort(a);
        System.out.println("排序后的数组为:"+Arrays.toString(a));
        int index = Arrays.binarySearch(a, 5);
        System.out.println("5在排序后数组的位置为:"+index);

    }
}

排序后的数组为:[1, 2, 3, 5, 8, 10]
5在排序后数组的位置为:3
原文地址:https://www.cnblogs.com/sweetorangezzz/p/12918617.html