Example usage for java.util Arrays binarySearch

List of usage examples for java.util Arrays binarySearch

Introduction

In this page you can find the example usage for java.util Arrays binarySearch.

Prototype

public static int binarySearch(Object[] a, Object key) 

Source Link

Document

Searches the specified array for the specified object using the binary search algorithm.

Usage

From source file:Main.java

public static void main(String[] argv) {
    String[] sortedArray = new String[] { "a", "b", "c", "d" };

    int index = Arrays.binarySearch(sortedArray, "c");

    index = Arrays.binarySearch(sortedArray, "e");

    int[] sortedIntArray = new int[] { 1, 2, 3, 5, 7 };

    index = Arrays.binarySearch(sortedIntArray, 6);
}

From source file:Main.java

public static void main(String[] argv) {

    String[] oldArray = new String[] { "a", "b", "c", "d" };

    int index = Arrays.binarySearch(oldArray, "e");
    if (index < 0) {
        int insertIndex = -index - 1;

        String[] newArray = new String[oldArray.length + 1];
        System.arraycopy(oldArray, 0, newArray, 0, insertIndex);
        System.arraycopy(oldArray, insertIndex, newArray, insertIndex + 1, oldArray.length - insertIndex);
        newArray[insertIndex] = "e";
        oldArray = newArray;/*  w  w  w .  j  a  v  a  2  s .  c  o m*/
    }
}

From source file:Main.java

public static void main(String[] args) {
    int items[] = { 5, 4, 6, 1, 8, 9, 6, 7, 3, 2 };

    Arrays.sort(items);// ww w  . j av  a2 s.  c o  m

    int index = Arrays.binarySearch(items, 1);

    System.out.println("Search item found at: " + index);
}

From source file:Main.java

public static void main(String[] args) {
    int intArray[] = { 1, 2, 4, 5 };
    Arrays.sort(intArray);/* w w w.  jav  a2s .c  o m*/
    int searchValue = 2;
    System.out.println(Arrays.binarySearch(intArray, searchValue));

    searchValue = 3;
    System.out.println(Arrays.binarySearch(intArray, searchValue));
}

From source file:Main.java

public static void main(String[] args) {
    byte bArray[] = { 1, 2, 4, 5 };
    Arrays.sort(bArray);// ww w .  j ava 2s  .co m

    byte searchValue = 2;

    int intResult = Arrays.binarySearch(bArray, searchValue);
    System.out.println("Result of binary search of 2 is : " + intResult);

    searchValue = 7;
    intResult = Arrays.binarySearch(bArray, searchValue);
    System.out.println("Result of binary search of 3 is : " + intResult);
}

From source file:Main.java

public static void main(String[] args) {
    short shortArray[] = { 1, 2, 4, 5 };
    Arrays.sort(shortArray);/*from w w w  . j  a  v  a 2 s .  c om*/

    short searchValue = 2;
    System.out.println(Arrays.binarySearch(shortArray, searchValue));

    searchValue = 3;
    System.out.println(Arrays.binarySearch(shortArray, searchValue));
}

From source file:Main.java

public static void main(String[] args) {
    long longArray[] = { 1L, 2L, 4L, 5L };
    Arrays.sort(longArray);//www.  j av  a  2 s . c o m

    long searchValue = 2L;
    System.out.println(Arrays.binarySearch(longArray, searchValue));

    searchValue = 3;
    System.out.println(Arrays.binarySearch(longArray, searchValue));
}

From source file:Main.java

public static void main(String[] args) {
    char charArray[] = { 'a', 'b', 'd', 'e' };
    Arrays.sort(charArray);/*from   w  w w.j  a v  a2  s.c  om*/

    char searchValue = 'b';
    System.out.println(Arrays.binarySearch(charArray, searchValue));

    searchValue = 'z';
    System.out.println(Arrays.binarySearch(charArray, searchValue));

}

From source file:Main.java

public static void main(String[] args) {
    float floatArray[] = { 1.2f, 2.1f, 4.7f, 5.3f };
    Arrays.sort(floatArray);/*from ww w.j ava  2 s .  c  om*/

    float searchValue = 4.7f;
    System.out.println(Arrays.binarySearch(floatArray, searchValue));

    searchValue = 3.3f;
    System.out.println(Arrays.binarySearch(floatArray, searchValue));
}

From source file:Main.java

public static void main(String[] args) {
    double doubleArray[] = { 1.3, 2.1, 4.7, 5.3 };
    Arrays.sort(doubleArray);//w  ww.  j a  v  a  2 s.  c o m

    double searchValue = 4.7;

    System.out.println(Arrays.binarySearch(doubleArray, searchValue));

    searchValue = 3.33;
    System.out.println(Arrays.binarySearch(doubleArray, searchValue));
}