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, int fromIndex, int toIndex, Object key) 

Source Link

Document

Searches a range of the specified array for the specified object using the binary search algorithm.

Usage

From source file:Main.java

public static void main(String[] args) {

    // initializing sorted array
    Object arr[] = { 1, 2, 22, 69, 111, 222 };

    // value to search
    int searchVal = 22;

    // entering range of index
    int retVal = Arrays.binarySearch(arr, 2, 4, searchVal);

    System.out.println("The index of element 22 is : " + retVal);
}

From source file:Main.java

public static void main(String[] args) {

    // initializing sorted int array
    int intArr[] = { 10, 20, 25, 32, 55 };

    // value to search
    int searchVal = 32;

    // entering range of index
    int retVal = Arrays.binarySearch(intArr, 1, 3, searchVal);

    System.out.println("The index of element 32 is : " + retVal);
}

From source file:Main.java

public static void main(String[] args) {

    // initializing sorted short array
    short shortArr[] = { 1, 2, 15, 52, 110 };

    // value to search
    short searchVal = 15;

    // entering range of index
    int retVal = Arrays.binarySearch(shortArr, 3, 4, searchVal);

    System.out.println("The index of element 15 is : " + retVal);
}

From source file:Main.java

public static void main(String[] args) {

    // initializing sorted float array
    float floatArr[] = { 5.2f, 5.3f, 5.9f, 6.3f };

    // value to search
    float searchVal = 6.3f;

    // entering range of index
    int retVal = Arrays.binarySearch(floatArr, 1, 4, searchVal);

    System.out.println("The index of element 6.3 is : " + retVal);
}

From source file:Main.java

public static void main(String[] args) {

    // initializing sorted char array
    char charArr[] = { 'a', 'c', 'd', 'e', 'f' };

    // value to search
    char searchVal = 'e';

    // entering the range of index
    int retVal = Arrays.binarySearch(charArr, 1, 5, searchVal);

    System.out.println("The index of e is : " + retVal);
}

From source file:Main.java

public static void main(String[] args) {

    // initializing sorted byte array
    byte byteArr[] = { 10, 20, 22, 35, 77 };

    // entering the value to be searched
    byte searchVal = 35;

    // the range of index
    int retVal = Arrays.binarySearch(byteArr, 2, 5, searchVal);

    System.out.println("The index of element 35 is : " + retVal);
}

From source file:Main.java

public static void main(String[] args) {

    // initializing sorted double array
    double doubleArr[] = { 5.4, 5.5, 9.2, 9.3, 35.4 };

    // value to search
    double searchVal = 5.4;

    // entering the range of index
    int retVal = Arrays.binarySearch(doubleArr, 1, 3, searchVal);

    System.out.println("The index of element 5.4 is : " + retVal);
}

From source file:Main.java

public static void main(String[] args) {

    // initializing sorted long array
    long longArr[] = { 123456789L, 1123456789L, 2123456789L, 3123456789L, 4123456789L };

    // value to search
    long searchVal = 2123456789L;

    // entering range of index
    int retVal = Arrays.binarySearch(longArr, 1, 2, searchVal);

    System.out.println(retVal);/*  w  w  w .ja v  a2s. co  m*/
}

From source file:Main.java

public static void insertInOrder(int n) {
    int pos = Arrays.binarySearch(array, 0, elements, n);
    if (pos < 0)
        pos = ~pos;/*from   w ww  .j  a va2s  .  c  o  m*/
    if (pos < elements)
        System.arraycopy(array, pos, array, pos + 1, elements - pos);
    array[pos] = n;
    elements++;
}

From source file:com.milaboratory.core.mutations.generator.SubstitutionModel.java

public int sample(RandomGenerator generator, int letter) {
    double value = generator.nextDouble();
    int index = Arrays.binarySearch(cdf, letter * size, (letter + 1) * size, value);
    if (index < 0)
        index = -index - 1;//www.  ja v a2s.  c om
    index -= letter * size;
    assert index < size;
    return index;
}