0
0
JavaHow-ToBeginner · 3 min read

How to Use Arrays.binarySearch in Java: Syntax and Examples

Use Arrays.binarySearch(array, key) to find the index of key in a sorted array. It returns the index if found, or a negative value if not found. The array must be sorted before calling this method.
📐

Syntax

The basic syntax of Arrays.binarySearch is:

  • Arrays.binarySearch(array, key): Searches the entire sorted array.
  • Arrays.binarySearch(array, fromIndex, toIndex, key): Searches a sorted range within the array.

Here, array is the sorted array to search, key is the value to find, and fromIndex and toIndex define the range to search (inclusive start, exclusive end).

java
int index = Arrays.binarySearch(array, key);
int indexRange = Arrays.binarySearch(array, fromIndex, toIndex, key);
💻

Example

This example shows how to use Arrays.binarySearch to find a number in a sorted array. It prints the index if found or a negative number if not.

java
import java.util.Arrays;

public class BinarySearchExample {
    public static void main(String[] args) {
        int[] numbers = {2, 4, 6, 8, 10, 12};
        int key = 8;
        int index = Arrays.binarySearch(numbers, key);

        if (index >= 0) {
            System.out.println("Found " + key + " at index " + index);
        } else {
            System.out.println(key + " not found. Returned: " + index);
        }
    }
}
Output
Found 8 at index 3
⚠️

Common Pitfalls

Common mistakes when using Arrays.binarySearch include:

  • Not sorting the array before searching, which leads to incorrect results.
  • Misinterpreting the negative return value, which indicates the insertion point as (-insertion_point - 1).
  • Using it on unsorted or partially sorted arrays.

Always sort the array first with Arrays.sort(array) before searching.

java
import java.util.Arrays;

public class BinarySearchPitfall {
    public static void main(String[] args) {
        int[] unsorted = {10, 2, 8, 4, 6};
        int key = 4;

        // Wrong: searching unsorted array
        int wrongIndex = Arrays.binarySearch(unsorted, key);
        System.out.println("Wrong index (unsorted): " + wrongIndex);

        // Correct: sort first
        Arrays.sort(unsorted);
        int correctIndex = Arrays.binarySearch(unsorted, key);
        System.out.println("Correct index (sorted): " + correctIndex);
    }
}
Output
Wrong index (unsorted): -2 Correct index (sorted): 1
📊

Quick Reference

  • Input: Sorted array and key to find.
  • Output: Index of key if found; otherwise, negative insertion point.
  • Sort first: Use Arrays.sort(array) before searching.
  • Range search: Use Arrays.binarySearch(array, fromIndex, toIndex, key) to search part of the array.

Key Takeaways

Always sort the array before using Arrays.binarySearch to get correct results.
Arrays.binarySearch returns the index if the key is found, or a negative value indicating the insertion point if not.
Use the overloaded method with fromIndex and toIndex to search within a specific range.
Negative return values mean the key is not present; calculate insertion point as -(return value + 1).
Do not use binarySearch on unsorted arrays as it leads to unpredictable results.