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) { byte bArray[] = { 1, 2, 4, 5 }; Arrays.sort(bArray);/*from w w w. j a v a 2 s .com*/ 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) { // initializing sorted long array long longArr[] = { 123456789L, 1123456789L, 2123456789L, 3123456789L, 4123456789L }; // value to search long searchVal = 2123456789L; int retVal = Arrays.binarySearch(longArr, searchVal); System.out.println(retVal);/*from w w w.j a va 2 s . c o m*/ }
From source file:Main.java
public static void main(String[] a) { String str[] = { "B", "H", "L", "M", "I", "N", "R" }; // Ensure list sorted Arrays.sort(str);/*from w w w .j av a2 s .co m*/ for (String s : str) { System.out.println(s); } // Search for element in list int index = Arrays.binarySearch(str, "M"); System.out.println("Found M @ " + index); }
From source file:Main.java
public static void main(String[] args) { int[] unsorted = { -3, 10, -4, 11, 5, 1, 2 }; System.out.println(Arrays.toString(unsorted)); int[] sorted = unsorted; Arrays.sort(sorted);/*from w ww . ja v a 2 s. c o m*/ System.out.println(Arrays.toString(sorted)); int breakingPoint = Arrays.binarySearch(sorted, 5); for (int i = breakingPoint; i < sorted.length; i++) { System.out.println(sorted[i]); } }
From source file:SearchTestDemo.java
public static void main(String args[]) throws Exception { int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 }; // Ensure array sorted Arrays.sort(array);/*from w ww .j ava 2s .co m*/ printArray("Sorted array", array); // Search for element in array int index = Arrays.binarySearch(array, 2); System.out.println("Found 2 @ " + index); // Search for element not in array index = Arrays.binarySearch(array, 1); System.out.println("Didn't find 1 @ " + index); // Insert int newIndex = -index - 1; array = insertElement(array, 1, newIndex); printArray("With 1 added", array); }
From source file:SearchTest.java
public static void main(String args[]) throws Exception { int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 }; // Ensure array sorted Arrays.sort(array);/*from w w w.j av a2s . c o m*/ printArray("Sorted array", array); // Search for element in array int index = Arrays.binarySearch(array, 2); System.out.println("Found 2 at " + index); // Search for element not in array index = Arrays.binarySearch(array, 1); System.out.println("Didn't find 1 at " + index); // Insert int newIndex = -index - 1; array = insertElement(array, 1, newIndex); printArray("With 1 added", array); }
From source file:ArraysDemo.java
public static void main(String args[]) { int array[] = new int[10]; for (int i = 0; i < 10; i++) array[i] = -3 * i;/*from w w w . jav a2 s.co m*/ display(array); Arrays.sort(array); System.out.print("Sorted: "); display(array); // Fill and display the array. Arrays.fill(array, 2, 6, -1); System.out.print("After fill(): "); display(array); // Sort and display the array. Arrays.sort(array); System.out.print("After sorting again: "); display(array); // Binary search for -9. System.out.print("The value -9 is at location "); int index = Arrays.binarySearch(array, -9); System.out.println(index); }
From source file:Main.java
public static void main(String[] args) { char charArray[] = { 'a', 'b', 'd', 'e' }; Arrays.sort(charArray);//ww w .j av a 2 s . c o m char searchValue = 'b'; System.out.println(Arrays.binarySearch(charArray, searchValue)); searchValue = 'z'; System.out.println(Arrays.binarySearch(charArray, searchValue)); }
From source file:MainClass.java
public static void main(String args[]) { int array[] = new int[10]; for (int i = 0; i < 10; i++) array[i] = 3 * i;/* w w w. ja v a 2s . c om*/ System.out.print("Original contents: "); display(array); Arrays.sort(array); System.out.print("Sorted: "); display(array); Arrays.fill(array, 2, 6, -1); System.out.print("After fill(): "); display(array); Arrays.sort(array); System.out.print("After sorting again: "); display(array); System.out.print("The value 9 is at location "); int index = Arrays.binarySearch(array, 9); System.out.println(index); }
From source file:Main.java
public static void main(String[] args) { double doubleArray[] = { 1.3, 2.1, 4.7, 5.3 }; Arrays.sort(doubleArray);/*from w ww . ja v a 2s. co m*/ double searchValue = 4.7; System.out.println(Arrays.binarySearch(doubleArray, searchValue)); searchValue = 3.33; System.out.println(Arrays.binarySearch(doubleArray, searchValue)); }