Arrays.binarySearch(short[], short): This method is available in java.util.Arrays class of Java.
Syntax:
int java.util.Arrays.binarySearch(short[] a, short key)
This method takes two arguments one of type short array and the other is of type short as its parameters. This method searches the specified array of shorts 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 ArraysbinarySearchshort {public static void main(String[] args) {// for short typeshort shortArray[] = { 55, 67, 345, 678, 890 };short shortKey = 67;// to ensure array is sortedArrays.sort(shortArray);System.out.println(Arrays.binarySearch(shortArray,shortKey));}}
Output:
1
No comments:
Post a Comment