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

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

Syntax:

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

This method takes four arguments one of type float array, the other two are of type int and the other one is of type int as its parameters. This method searches a range ofthe specified array of floats 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 are 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 ArraysbinarySearchfloatRange {
    public static void main(String[] args) {

        // for type float
        float floatArray[] = { 13.4f, 14.5f, 56.6f, 78.78f };

        int fromIndex = 0, toIndex = 4;
        float floatKey = 14.5f;

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

Output:

1


Approach 2: IllegalArgumentException 

Java

import java.util.Arrays;

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

        // for type float
        float floatArray[] = { 13.4f, 14.5f, 56.6f, 78.78f };

        int fromIndex = 5, toIndex = 4;
        float floatKey = 14.5f;

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

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:2112)



Approach 3: ArrayIndexOutOfBoundsException

Java

import java.util.Arrays;

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

        // for type float
        float floatArray[] = { 13.4f, 14.5f, 56.6f, 78.78f };

        int fromIndex = 0, toIndex = 6;
        float floatKey = 14.5f;

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

Output:

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


No comments:

Post a Comment