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[] args) {
  float floatArray[] = { 1.2f, 2.1f, 4.7f, 5.3f };
  Arrays.sort(floatArray);/*from   w w  w .  j  ava2  s.c  o m*/

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

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

From source file:ProxyTest.java

public static void main(String[] args) {
    Object[] elements = new Object[1000];

    // fill elements with proxies for the integers 1 ... 1000
    for (int i = 0; i < elements.length; i++) {
        Integer value = i + 1;//from   www. j av a 2s. c  o  m
        InvocationHandler handler = new TraceHandler(value);
        Object proxy = Proxy.newProxyInstance(null, new Class[] { Comparable.class }, handler);
        elements[i] = proxy;
    }

    // construct a random integer
    Integer key = new Random().nextInt(elements.length) + 1;

    // search for the key
    int result = Arrays.binarySearch(elements, key);

    // print match if found
    if (result >= 0)
        System.out.println(elements[result]);
}

From source file:ArraysTester.java

public static void main(String[] args) {
    ArraysTester tester = new ArraysTester(50);
    int[] myArray = tester.get();

    // Compare two arrays
    int[] myOtherArray = tester.get().clone();
    if (Arrays.equals(myArray, myOtherArray)) {
        System.out.println("The two arrays are equal!");
    } else {//from  w  w w .ja  va2  s  . c o m
        System.out.println("The two arrays are not equal!");
    }

    // Fill up some values
    Arrays.fill(myOtherArray, 2, 10, new Double(Math.PI).intValue());
    myArray[30] = 98;

    // Print array, as is
    System.out.println("Here's the unsorted array...");
    System.out.println(Arrays.toString(myArray));
    System.out.println();

    // Sort the array
    Arrays.sort(myArray);

    // print array, sorted
    System.out.println("Here's the sorted array...");
    System.out.println(Arrays.toString(myArray));
    System.out.println();

    // Get the index of a particular value
    int index = Arrays.binarySearch(myArray, 98);
    System.out.println("98 is located in the array at index " + index);

    String[][] ticTacToe = { { "X", "O", "O" }, { "O", "X", "X" }, { "X", "O", "X" } };
    System.out.println(Arrays.deepToString(ticTacToe));

    String[][] ticTacToe2 = { { "O", "O", "X" }, { "O", "X", "X" }, { "X", "O", "X" } };

    String[][] ticTacToe3 = { { "X", "O", "O" }, { "O", "X", "X" }, { "X", "O", "X" } };

    if (Arrays.deepEquals(ticTacToe, ticTacToe2)) {
        System.out.println("Boards 1 and 2 are equal.");
    } else {
        System.out.println("Boards 1 and 2 are not equal.");
    }

    if (Arrays.deepEquals(ticTacToe, ticTacToe3)) {
        System.out.println("Boards 1 and 3 are equal.");
    } else {
        System.out.println("Boards 1 and 3 are not equal.");
    }
}

From source file:Main.java

  public static void main(String[] args) {
  int intArray[] = { 1, 2, 4, 5 };
  Arrays.sort(intArray);/*from w w w.jav  a 2s.  c om*/
  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) {
  long longArray[] = { 1L, 2L, 4L, 5L };
  Arrays.sort(longArray);/*from   ww  w  .  ja  va 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) {
        short shortArray[] = { 1, 2, 4, 5 };
        Arrays.sort(shortArray);/*from  w  ww. ja v  a 2s  .co m*/

        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 boolean useArraysBinarySearch(String[] arr, String targetValue) {
    int a = Arrays.binarySearch(arr, targetValue);
    if (a >= 0) {
        return true;
    } else {//from  w  ww  . j  av a2 s. c  o  m
        return false;
    }
}

From source file:Main.java

public static double[] merge(double[] a, double[] b) {
    int N = a.length;
    int M = b.length;
    int size = M + N;
    for (double elementB : b) {
        if (Arrays.binarySearch(a, elementB) >= 0) {
            size--;//  ww  w  .  j  av  a 2 s.c o m
        }
    }
    double[] c = new double[size];
    for (int i = 0; i < a.length; i++) {
        c[i] = a[i];
    }
    for (int i = a.length, j = 0; j < b.length; j++) {
        if (Arrays.binarySearch(c, b[j]) < 0) {
            c[i] = b[j];
            i++;
        }
    }
    Arrays.sort(c);
    return c;
}

From source file:Main.java

public static String stripCharacters(String in, char[] character) {
    if (in == null || ("".equals(in)))
        return "";
    StringBuffer out = new StringBuffer();
    char current;
    for (int i = 0; i < in.length(); i++) {
        current = in.charAt(i);/*from w ww  . j  a  va 2  s  .  c om*/
        if (Arrays.binarySearch(character, current) < 0) {
            out.append(current);
        }
    }
    return out.toString();
}

From source file:SetMembership.java

static boolean isKeyword2(String id) {
    return Arrays.binarySearch(keywordarray, id) >= 0;
}