Arrays.binarySearch(char[], int, int, char) in Java

Arrays.binarySearch(char[], int, int, char): This method is available in java.util.Arrays class of Java.

Syntax:

int java.util.Arrays.binarySearch(char[] a, int fromIndex, int toIndex, char key)

This method takes four arguments one of type char array and two are of type int and one of type char as its parameters. This method searches a range of the specified array of chars for the specified value.

Note: The range must be sorted

Parameters: Four parameters are required for this method.

a: the array to be searched.

fromIndex: the index of the first element (inclusive) to be searched.

toIndex: the index of the last element (exclusive) to be searched.

key: the value to be searched for.

Returns: index of the search key, if it is contained in the array within the specified range; otherwise, (-(insertion point) - 1).

Note: The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range is less than the specified key.

Throws:

1. IllegalArgumentException - if fromIndex > toIndex.

2. ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length

Approach 1: When no exceptions.

Java

import java.util.Arrays;

public class ArraysbinarySearchcharRange {
    public static void main(String[] args) {

        int fromIndex = 2, toIndex = 4;

        // for char type
        char charArray[] = { 'a', 'd', 'f', 'h', 'o' };
        char charKey = 'f';

        // to ensure array is sorted
        Arrays.sort(charArray);
        System.out.println(Arrays.binarySearch(charArray,
fromIndex, toIndex, charKey));
    }
}

Output:

2


Approach 2: IllegalArgumentException 

Java

import java.util.Arrays;

public class ArraysbinarySearchcharRange {
    public static void main(String[] args) {

        // for char type
        char charArray[] = { 'a', 'd', 'f', 'h', 'o' };

        int fromIndex = 5, toIndex = 4;
        char charKey = 'f';

        // to ensure array is sorted
        Arrays.sort(charArray);
        System.out.println(Arrays.binarySearch(charArray,
fromIndex, toIndex, charKey));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(5) > toIndex(4) at java.base/java.util.Arrays.rangeCheck(Arrays.java:718) at java.base/java.util.Arrays.binarySearch(Arrays.java:1857)



Approach 3: ArrayIndexOutOfBoundsException 

Java

import java.util.Arrays;

public class ArraysbinarySearchcharRange {
    public static void main(String[] args) {

        // for char type
        char charArray[] = { 'a', 'd', 'f', 'h', 'o' };

        int fromIndex = 2, toIndex = 7;
        char charKey = 'f';

        // to ensure array is sorted
        Arrays.sort(charArray);
        System.out.println(Arrays.binarySearch(charArray,
fromIndex, toIndex, charKey));
    }
}

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 7 at java.base/java.util.Arrays.rangeCheck(Arrays.java:725) at java.base/java.util.Arrays.binarySearch(Arrays.java:1857)


No comments:

Post a Comment