List of usage examples for java.util Arrays binarySearch
public static int binarySearch(Object[] a, Object key)
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; int retVal = Arrays.binarySearch(intArr, searchVal); System.out.println("The index of element 32 is : " + retVal); }
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; int retVal = Arrays.binarySearch(arr, searchVal); System.out.println("The index of element 22 is : " + retVal); }
From source file:Main.java
public static void main(String[] argv) throws Exception { // Create an array with an ordered list of numbers int[] sortedIntArray = new int[] { 1, 2, 3, 5, 7 }; // Search for 6 int index = Arrays.binarySearch(sortedIntArray, 6); System.out.println(index);/*from w w w .j a v a2 s . c o m*/ }
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; int retVal = Arrays.binarySearch(shortArr, searchVal); System.out.println("The index of element 15 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'; int retVal = Arrays.binarySearch(charArr, 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, 66 }; // the value to be searched byte searchVal = 35; int retVal = Arrays.binarySearch(byteArr, searchVal); System.out.println("The index of element 35 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; int retVal = Arrays.binarySearch(floatArr, 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 double array double doubleArr[] = { 5.4, 5.5, 9.2, 9.3, 35.4 }; // value search double searchVal = 5.4; int retVal = Arrays.binarySearch(doubleArr, searchVal); System.out.println("The index of element 5.4 is : " + retVal); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 }; Arrays.sort(array);/*from ww w . ja v a 2s . c o m*/ printArray("Sorted array", array); int index = Arrays.binarySearch(array, 2); System.out.println("Found 2 @ " + index); index = Arrays.binarySearch(array, 1); System.out.println("Didn't find 1 @ " + index); int newIndex = -index - 1; array = insertElement(array, 1, newIndex); printArray("With 1 added", array); }
From source file:Main.java
public static void main(String[] argv) throws Exception { // Create an array with an ordered list of strings String[] sortedArray = new String[] { "ant", "bat", "cat", "dog" }; // Search for the word "cat" int index = Arrays.binarySearch(sortedArray, "cat"); // 2 // Search for a non-existent element index = Arrays.binarySearch(sortedArray, "cow"); // -4 }