Home  >  Article  >  Java  >  Java documentation interpretation: detailed description of the binarySearch() method of the Arrays class

Java documentation interpretation: detailed description of the binarySearch() method of the Arrays class

PHPz
PHPzOriginal
2023-11-03 14:35:36670browse

Java documentation interpretation: detailed description of the binarySearch() method of the Arrays class

Interpretation of Java documentation: Detailed description of the binarySearch() method of the Arrays class, specific code examples are required

In Java, the Arrays class provides many convenient methods to operate array. One of them is the binarySearch() method, which can be used to find the index of a specified element in a sorted array. This article will introduce the binarySearch() method in the Arrays class in detail and provide specific code examples to illustrate its usage.

The syntax of the binarySearch() method

The syntax of the binarySearch() method is as follows:

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

Where, a is a sorted array, key is the element to be found.

Return value of the binarySearch() method

If the element is found, this method returns the index of the element; otherwise, a negative number will be returned, which is the position where the element should be inserted. You can use~ Negative number to calculate the position of inserted elements.

Basic principles of binary search

Before introducing the specific usage of the binarySearch() method, let’s first understand the basic principles of binary search.

Binary search is a search algorithm used to find specific elements in an ordered array. The basic idea is to divide the array into two parts and then compare the element you are looking for to the middle element. Based on the comparison, you can determine where in the array the element you are looking for is. You can then proceed with a binary search on this part to find the specific element in less time.

Specific usage of the binarySearch() method

Before using the binarySearch() method, you must ensure that the array has been sorted. If the array is not sorted, the result will be undefined.

The following example demonstrates how to use the binarySearch() method to find a specific element in an array.

import java.util.Arrays;
  
public class BinarySearchExample {
    public static void main(String[] args) {
        int a[] = { 10, 20, 15, 22, 35 };
        Arrays.sort(a);
        System.out.println("Sorted array :: " + Arrays.toString(a));
 
        int key = 22;
 
        int result = Arrays.binarySearch(a, key);
 
        if (result < 0)
            System.out.println(key + " was not found in the array.");
        else
            System.out.println(key + " was found at index " + result);
    }
}

Executing this code will output the following:

Sorted array :: [10, 15, 20, 22, 35]
22 was found at index 3

In this example, we first define an array of integers a. We use the sort() method of the Arrays class to sort the array and then find a specific element key in the array. We use the binarySearch() method to find the index of element key and print the result on the console.

If the element to be found is not in the array, the binarySearch() method will return a negative number, indicating where the element should be inserted into the array to maintain the ascending order of the array. For example, if we change the above example to find element 25, the output will be:

Sorted array :: [10, 15, 20, 22, 35]
25 was not found in the array.

In this example, binarySearch(a, key) returns -5, if we use ~ -5 to convert it to the position where the element is inserted, we get 4, which means if we want to insert the element 25 into into the array, it should be inserted at index 4.

Summary

In this article, we have provided a detailed explanation of the binarySearch() method of the Arrays class and demonstrated its usage. Although this method is very simple, it is very useful in many practical applications because it can quickly find a specific element in a sorted array. If you need to find elements in a sorted array, try using the binarySearch() method.

The above is the detailed content of Java documentation interpretation: detailed description of the binarySearch() method of the Arrays class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn