Java Arrays.binarySearch()
方法
原文:https://www.studytonight.com/java-util/java-arrays-binarysearch-method
在本教程中,我们将通过 Java 中的一些好例子来探索binarySearch()
。这个方法属于 Java 中的Arrays
类,对于在大数组中搜索一个键非常有帮助。我们都知道二分搜索法的工作原理是分而治之,所以搜索效率很高。
语法
binarySearch(T[] a, T key, Comparator<? super T> c)
binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)
第一个给定的语法是针对二分搜索法的,当我们想要在整个数组中找到键的时候。我们将第一个参数作为我们想要搜索的数组传递,第二个参数将是我们想要搜索的关键字,第三个参数将是设置数组排序顺序的Comparator
。默认情况下,它会认为数组是按升序排序的。
在第二种语法中,我们得到了一个数组,但是我们不想对整个数组执行二分搜索法运算,在这种情况下,我们可以传递开头的 索引和结尾的索引。通过使用这些索引,该方法为执行二分搜索法操作提供了很大的灵活性。
binarySearch()
方法重载方法列表
此表包含binarySearch()
方法的所有重载变体。
方法 | 描述 |
---|---|
静态 int binarySearch(字节[] a,字节键) | 此方法使用二分搜索法算法在指定的字节数组中搜索指定的值。 |
静态 int binarySearch(字节[] a,int fromIndex,int toIndex,字节键) | 此方法使用二分搜索法算法在指定的字节数组范围内搜索指定的值。 |
静态 int binarySearch(char[] a,char 键) | 此方法使用二分搜索法算法在指定的字符数组中搜索指定的值。 |
静态 int binarySearch(char[] a,int fromIndex,int toIndex,char key) | 此方法使用二分搜索法算法在指定的字符数组范围内搜索指定的值。 |
静态 int binarySearch(double[] a,double key) | 此方法使用二分搜索法算法在指定的双精度数组中搜索指定的值。 |
静态 int binarySearch(double[] a,int fromIndex,int toIndex,double key) | 此方法使用二分搜索法算法在指定的双精度数组范围内搜索指定的值。 |
静态 int binarySearch(float[] a,float 键) | 此方法使用二分搜索法算法在指定的浮点数组中搜索指定的值。 |
静态 int binarySearch(float[] a,int fromIndex,int toIndex,float key) | 此方法使用二分搜索法算法在指定的浮点数组范围内搜索指定的值。 |
静态 int binarySearch(int[] a,int key) | 此方法使用二分搜索法算法在指定的 int 数组中搜索指定的值。 |
静态 int binarySearch(int[] a,int fromIndex,int toIndex,int key) | 此方法使用二分搜索法算法在指定的整数数组范围内搜索指定的值。 |
静态 int binarySearch(long[] a,int fromIndex,int toIndex,long key) | 此方法使用二分搜索法算法在指定的长数组范围内搜索指定的值。 |
静态 int binarySearch(长[] a,长键) | 此方法使用二分搜索法算法在指定的长数组中搜索指定的值。 |
静态 int binarySearch(短[] a,int fromIndex,int toIndex,短键) | 此方法使用二分搜索法算法在指定的短路数组范围内搜索指定的值。 |
静态 int binarySearch(短[] a,短键) | 此方法使用二分搜索法算法在指定的短路数组中搜索指定的值。 |
静态 int binarySearch(对象[] a,int fromIndex,int toIndex,对象键) | 此方法使用二分搜索法算法在指定数组的范围内搜索指定对象。 |
静态 int binarySearch(对象[] a,对象键) | 此方法使用二分搜索法算法在指定的数组中搜索指定的对象。 |
静态< T >内部二进制搜索(T[] a,**int fromIndex,int toIndex,T 键,Comparator <?超级 T > c)** |
此方法使用二分搜索法算法在指定数组的范围内搜索指定对象。 |
静态< T >内部二进制搜索(T[] a,**T 键,Comparator <?超级 T > c)** |
此方法使用二分搜索法算法在指定的数组中搜索指定的对象。 |
示例:没有Comparator
的binarySearch()
在下面给出的例子中,我们在一个按升序排序的数组上使用Arrays.binarySearch()
。此方法将返回键元素的索引。
import java.util.Arrays;
public class StudyTonight
{
public static void main(String[] args)
{
int array[]={2,6,7,8,13,19,27,55,80};
int key = 19;
int index = Arrays.binarySearch(array,key);
System.out.print(key+" found at index: "+index);
}
}
19 在索引:5 处找到
示例:带Comparator
的binarySearch()
有时可能会发生数组以非递减方式排序的情况,在这种情况下,我们需要传递一个Comparator
函数,然后它将像正常的二分搜索法一样工作。在下面给出的代码中,您可以看到这个函数正在返回 33 的索引,它是正确的 4,而不考虑排序的顺序,只是因为Comparator
函数。
import java.util.Arrays;
import java.util.Comparator;
public class StudyTonight
{
public static void main(String[] args)
{
Integer array[]={90, 55, 49, 42, 33, 25, 8, 6, 1};
Integer key = 33;
int index = Arrays.binarySearch(array,key,new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
if(o1>o2)
{
return 1;
}
else
{
return 0;
}
}
});
System.out.print(key+" found at index: "+index);
}
}
33 在索引:4 处找到
示例:带Comparator
的binarySearch()
如果我们不想在整个数组上执行二分搜索法呢?在这种情况下,我们必须将开始索引、fromIndex 和结束索引传递给 index,然后 binarySearch()方法将处理一切。在下面给出的代码中,二分搜索法将从 indec 2 执行到 indec 7。
import java.util.Arrays;
public class StudyTonight
{
public static void main(String[] args)
{
int array[]={2,6,7,8,13,19,27,55,80};
int key = 19;
int index = Arrays.binarySearch(array,2,7,key);
System.out.print(key+" found at index: "+index);
}
}
19 在索引:5 处找到
二分搜索法总是按照排序的顺序工作,可能是升序,也可能是降序。它非常高效,因为它基于分治原则。
示例:binarySearch()
及其重载方法
在下面给出的例子中,我们实现了binarySearch()
方法的所有重载方法。我们可以看到所有的数组元素都已经排序了,因为二分搜索法只使用排序数组。
import java.util.Arrays;
public class StudyTonight
{
public static void main(String[] args)
{
byte byteArray[] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60};
char charArray[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'};
int intArray[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150};
double doubleArray[] = {5.1, 6.2, 7.2, 8.1, 9.4, 10.2, 11.6, 12.96, 13.2, 14.25, 15.6, 16.4, 17.2};
float floatArray[] = {1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f};
short shortArray[] = {2, 4, 6, 8, 10 ,12, 14, 16, 18, 20};
byte byteKey = 25;
//Example of static int binarySearch(byte[] a, byte key)
System.out.println(byteKey + " found at index = "+Arrays.binarySearch(byteArray,byteKey));
//Example of static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key)
System.out.println(byteKey + " found at index = "+Arrays.binarySearch(byteArray,0,7,byteKey));
char charKey = 'd';
//Example of static int binarySearch(char[] a, char key)
System.out.println(charKey + " found at index = "+Arrays.binarySearch(charArray,charKey));
//Example of static int binarySearch(char[] a, int fromIndex, int toIndex, char key)
System.out.println(charKey + " found at index = "+Arrays.binarySearch(charArray, 3, 8, charKey));
int intKey = 130;
//Example of static int binarySearch(int[] a, int key)
System.out.println(intKey + " found at index = "+Arrays.binarySearch(intArray, intKey));
//Example of static int binarySearch(int[] a, int fromIndex, int toIndex, int key)
System.out.println(intKey + " found at index = "+Arrays.binarySearch(intArray, 0, 5, intKey));
double doubleKey = 15.6;
//Example of static int binarySearch(double[] a, double key)
System.out.println(doubleKey + " found at index = "+Arrays.binarySearch(doubleArray,doubleKey));
//Example of static int binarySearch(double[] a, int fromIndex, int toIndex, double key)
System.out.println(doubleKey + " found at index = "+Arrays.binarySearch(doubleArray, 1, 5, doubleKey));
float floatKey = 9f;
//Example of static int binarySearch(float[] a, float key)
System.out.println(floatKey + " found at index = "+Arrays.binarySearch(floatArray, floatKey));
//Example of static int binarySearch(float[] a, int fromIndex, int toIndex, float key)
System.out.println(floatKey + " found at index = "+Arrays.binarySearch(floatArray, 0, 5, floatKey));
short shortKey = 16;
//Example of static int binarySearch(short[] a, short key)
System.out.println(shortKey + " found at index = "+Arrays.binarySearch(shortArray, shortKey));
//Example of static int binarySearch(short[] a, int fromIndex, int toIndex, short key)
System.out.println(shortKey + " found at index = "+Arrays.binarySearch(shortArray, 2, 8, shortKey));
}
}
25 在索引= 4 处发现 25 在索引= 4 处发现 d 在索引= 3 处发现 d 在索引= 3 处发现 130 在索引= 12 处发现 130 在索引= -6 处发现 15.6 在索引= 10 处发现 15.6 在索引= -6 处发现 9.0 在索引= 8 处发现 9.0 在索引= -6 处发现 16 在索引= 7 处发现【T11
结论:
binarySearch()方法有不同的变体,比如我们可以按升序和降序传递数组,这个函数给了我们通过传递起始索引和结束索引来限制数组某些部分的能力。