Java Arrays binarySearch() 方法



描述

Java Arrays binarySearch(char[] a, char key) 方法使用二分查找算法在指定的字符数组中搜索指定的值。在调用此方法之前,数组必须已排序。如果未排序,则结果未定义。

声明

以下是 java.util.Arrays.binarySearch(char[] a, char key) 方法的声明

public static int binarySearch(char[] a, char key)

参数

  • a − 要搜索的数组。

  • key − 要搜索的值。

返回值

如果搜索键包含在数组中,则此方法返回搜索键的索引,否则返回 (-(插入点) - 1)。插入点是键将插入数组的位置:大于键的第一个元素的索引,如果数组中的所有元素都小于指定的键,则为 a.length。

异常

Java Arrays binarySearch(char[] a, int fromIndex, int toIndex, char key) 方法

描述

Java Arrays binarySearch(char[] a, int fromIndex, int toIndex, char key) 方法使用二分查找算法在指定的字符数组的指定范围内搜索指定的值。在调用此方法之前,该范围必须已排序。如果未排序,则结果未定义。

声明

以下是 java.util.Arrays.binarySearch(char[] a, int fromIndex, int toIndex, char key) 方法的声明

public static int binarySearch(char[] a, int fromIndex, int toIndex, char key)

参数

  • a − 要搜索的数组。

  • fromIndex − 要搜索的第一个元素(包含)的索引。

  • toIndex − 要搜索的最后一个元素(不包含)的索引。

  • key − 要搜索的值。

返回值

如果搜索键包含在数组中,则此方法返回搜索键的索引,否则返回 (-(插入点) - 1)。插入点是键将插入数组的位置;该范围内大于键的第一个元素的索引,如果该范围内的所有元素都小于指定的键,则为 toIndex。

异常

  • IllegalArgumentException − 如果 fromIndex > toIndex

  • ArrayIndexOutOfBoundsException − 如果 fromIndex < 0 或 toIndex > a.length

在字符数组上执行二分查找示例

以下示例显示了 Java Arrays binarySearch(char[], key) 方法的使用。首先,我们创建了一个字符数组,对其进行了排序并打印。然后对一个值执行二分查找并打印结果。

package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {

      // initializing unsorted char array
      char charArr[] = {'a', 'c', 'b', 'e','d'};

      // sorting array
      Arrays.sort(charArr);

      // let us print all the elements available in list
      System.out.println("The sorted char array is:");
      for (char ch : charArr) {
         System.out.println("Char = " + ch);
      }

      // entering the value to be searched
      char searchVal = 'e';
      int retVal = Arrays.binarySearch(charArr,searchVal);
      System.out.println("The index of element e is : " + retVal);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

The sorted char array is:
Char = a
Char = b
Char = c
Char = d
Char = e
The index of element e is : 4

在字符子数组上执行二分查找示例

以下示例显示了 Java Arrays binarySearch(char[], fromIndex, toIndex, key) 方法的使用。首先,我们创建了一个字符数组,对其进行了排序并打印。然后对子数组上的一个值执行二分查找并打印结果。

package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {

      // initializing unsorted char array
      char charArr[] = {'a', 'c', 'b', 'e','d'};

      // sorting array
      Arrays.sort(charArr);

      // let us print all the elements available in list
      System.out.println("The sorted char array is:");
      for (char ch : charArr) {
         System.out.println("Char = " + ch);
      }

      // entering the value to be searched
      char searchVal = 'e';

      // entering the range of index
      int retVal = Arrays.binarySearch(charArr,2,5,searchVal);
      System.out.println("The index of element e is : " + retVal);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

The sorted char array is:
Char = a
Char = b
Char = c
Char = d
Char = e
The index of element e is : 4

在字符数组上对不存在的值执行二分查找示例

以下示例显示了 Java Arrays binarySearch(char[], key) 方法的使用。首先,我们创建了一个字符数组,对其进行了排序并打印。然后对数组中不存在的值执行二分查找并打印结果。

package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {

      // initializing unsorted char array
      char charArr[] = {'a', 'c', 'b', 'e','d'};

      // sorting array
      Arrays.sort(charArr);

      // let us print all the elements available in list
      System.out.println("The sorted char array is:");
      for (char ch : charArr) {
         System.out.println("Char = " + ch);
      }

      // entering the value to be searched
      char searchVal = 'f';
      int retVal = Arrays.binarySearch(charArr,searchVal);
      System.out.println("The index of element f is : " + retVal);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

The sorted char array is:
Char = a
Char = b
Char = c
Char = d
Char = e
The index of element f is : -6
java_util_arrays.htm
广告