📜  Java中的 Arrays.binarySearch() 示例 |设置 1

📅  最后修改于: 2022-05-13 01:55:36.261000             🧑  作者: Mango

Java中的 Arrays.binarySearch() 示例 |设置 1

Arrays.binarySearch()方法使用二进制搜索算法在给定数据类型的指定数组中搜索指定值。在进行此调用之前,必须按照 Arrays.sort() 方法对数组进行排序。如果未排序,则结果未定义。如果数组包含多个具有指定值的元素,则无法保证会找到哪一个。让我们滑过下面提供的插图,如下所示。

插图:

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 0 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

句法:

public static int binarySearch(data_type arr, data_type key)

参数:

  • 要搜索的数组
  • 要搜索的值

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

需要牢记以下几点:

  • 如果输入列表未排序,则结果未定义。
  • 如果有重复,则无法保证会找到哪一个。

如上所述,我们已经讨论过我们可以使用Arrays.binarysearch() 和Collections.binarysearch()来操作这个算法 Arrays.binarysearch() 适用于也可以是原始数据类型的数组。 Collections.binarysearch() 适用于对象集合,如 ArrayList 和 LinkedList。

示例 1:

Java
// Java program to demonstrate working of Arrays.
// binarySearch() in a sorted array
 
// Importing Arrays class from
// java.util package
import java.util.Arrays;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing byte arrays
        // to search over them
        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 };
 
        // Using sort() method of Arrays class
        // and passing arrays to be sorted as in arguments
        Arrays.sort(byteArr);
        Arrays.sort(charArr);
        Arrays.sort(intArr);
        Arrays.sort(doubleArr);
        Arrays.sort(floatArr);
        Arrays.sort(shortArr);
 
        // Primitive datatypes
        byte byteKey = 35;
        char charKey = 'g';
        int intKey = 22;
        double doubleKey = 1.5;
        float floatKey = 35;
        short shortKey = 5;
 
        // Now in sorted array we will fetch and
        // return elements/indiciesaccessing indexes to show
        // array is really sorted
 
        // Print commands where we are implementing
        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));
    }
}


Java
// Java Program to Illustrate binarySearch() method
//  of  Collections class
 
// Importing required classes
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating empty List
        List al = new ArrayList();
 
        // Adding elements to the List
        al.add(12);
        al.add(53);
        al.add(23);
        al.add(46);
        al.add(54);
 
        // Using binarySearch() method of Collections class
        // over random inserted element and storing the
        // index
        int index = Collections.binarySearch(al, 23);
 
        // Print and display the index
        System.out.print(index);
    }
}


输出
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

此方法有多种变体,我们还可以在其中指定要搜索的数组范围。我们将在以后的文章中讨论这一点以及在 Object 数组中搜索。

示例 2:

Java

// Java Program to Illustrate binarySearch() method
//  of  Collections class
 
// Importing required classes
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating empty List
        List al = new ArrayList();
 
        // Adding elements to the List
        al.add(12);
        al.add(53);
        al.add(23);
        al.add(46);
        al.add(54);
 
        // Using binarySearch() method of Collections class
        // over random inserted element and storing the
        // index
        int index = Collections.binarySearch(al, 23);
 
        // Print and display the index
        System.out.print(index);
    }
}
输出
2