Arrays.binarySearch(double[], double): This method is available in java.util.Arrays class of Java.
Syntax:
int java.util.Arrays.binarySearch(double[] a, double key)
This method takes two arguments one of type double array and the other of type double as its parameters. This method searches the specified array of doubles for the specified value.
Note: The array must be sorted
Parameters: Two parameters are required for this method.
a: the array to be searched.
key: the value to be searched for.
Returns: index of the search key, if it is contained in the array; 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 greater than the key, or a.length if all elements in the array are less than the specified key.
Exceptions: NA
Approach
Java
import java.util.Arrays;public class ArraysbinarySearchdouble {public static void main(String[] args) {// for type doubledouble doubleArray[] = { 13.45, 45.67, 234.89, 2727.90 };double doubleKey = 234.89;// to ensure array is sortedArrays.sort(doubleArray);System.out.println(Arrays.binarySearch(doubleArray,doubleKey));}}
Output:
2
No comments:
Post a Comment