Java binarySearch()

The binarySearch() method implements the binary search algorithm to search the element passed as an argument. If you want to learn about how binary search works, visit Binary search algorithm.

Note: If we need to implement the binary search algorithm in Java, it is better to use the binarySearch() method rather than implementing the algorithm on our own.


Example: Java binarySearch()

Output

ArrayList: [2, 3, 4]
Position of 3: 1