Java Arrays.binarySearch() Example - Searching an Array

Java Arrays class provides many overloaded binarySearch() methods to search the specified array for the specified object using the binary search algorithm.
  • static int binarySearch(byte[] a, byte key) - Searches the specified array of bytes for the specified value using the binary search algorithm.
  • static int binarySearch(char[] a, char key) - Searches the specified array of chars for the specified value using the binary search algorithm.
  • static int binarySearch(double[] a, double key) - Searches the specified array of doubles for the specified value using the binary search algorithm.
  • static int binarySearch(float[] a, float key) - Searches the specified array of floats for the specified value using the binary search algorithm.
  • static int binarySearch(int[] a, int key) - Searches the specified array of ints for the specified value using the binary search algorithm.
  • static int binarySearch(long[] a, long key) - Searches the specified array of longs for the specified value using the binary search algorithm.
  • static int binarySearch(Object[] a, Object key) - Searches the specified array for the specified object using the binary search algorithm.
  • static int binarySearch(short[] a, short key) - Searches the specified array of shorts for the specified value using the binary search algorithm.
  • static int binarySearch(T[] a, T key, Comparator<? super T> c) - Searches the specified array for the specified object using the binary search algorithm.

Java Arrays.binarySearch() Example - Searching an Array

This example demonstrates the usage of above all search methods.
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * java.util.Arrays Class API examples
 * 
 * @author javaguides.net
 *
 */
public class ArraysJavaUtilClass {

        public static void main(String[] args) {

                // Searches the specified array for the specified String using the
                // binary search algorithm.
                final String key = "abc";
                String[] strArray = { "abc", "cdf", "pqr" };
                int index = Arrays.binarySearch(strArray, key);
                System.out.println(" String key found at index : " + index);

                // Searches the specified array of ints for the specified value using
                // the binary search algorithm.
                int[] intArray = { 1, 2, 3, 4 };
                index = Arrays.binarySearch(intArray, 3);
                System.out.println(" String key found at index : " + index);
                // Searches the specified array of bytes for the specified value using
                // the binary search algorithm.
                byte k = 1;
                byte[] byteArray = { 1, 2, 3, 4, 5 };
                Arrays.binarySearch(byteArray, k);

                // Searches the specified array of chars for the specified value using
                // the binary search algorithm.
                char charKey = 'a';
                char[] charArray = { 'a', 'b', 'c' };
                Arrays.binarySearch(charArray, charKey);

                // Searches the specified array of doubles for the specified value using
                // the binary search algorithm.
                double[] doubleArray = { 0.1, 0.2, 0.3 };
                Arrays.binarySearch(doubleArray, 0.2);

                // Searches the specified array of longs for the specified value using
                // the binary search algorithm.
                long[] longArray = { 1, 2, 3, 4, 5 };
                Arrays.binarySearch(longArray, 1);

                // Searches the specified array of floats for the specified value using
                // the binary search algorithm
                float[] floatArray = { 1, 2, 3, 4, 5 };
                Arrays.binarySearch(floatArray, 2);
                
        }
}

Output:
 String key found at index : 0
 String key found at index : 2

Reference


Comments