Arrays.binarySearch(byte[], byte): This method is available in java.util.Arrays class of Java.
Syntax:
int java.util.Arrays.binarySearch(byte[] a, byte key)
This method takes two arguments one of type byte array and the other value of type byte as its parameters. This method searches the specified array of bytes 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). 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 ArraysbinarySearchbyte {public static void main(String[] args) {// for byte typebyte byteArray[] = { 1, 2, 4, 5, 6 };byte byteKey = 4;// to ensure array is sortedArrays.sort(byteArray);System.out.println(Arrays.binarySearch(byteArray,byteKey));}}
Output:
2
No comments:
Post a Comment