Array, searching element
java.util.Arrays.binarySearch
If you want to find an element in a sorted Array you can use java.util.Arrays.binarySearch(..):
String[] sortedArray = new String[]{"Apfel", "Birne", "Kiwi"}; // Search for Birne: int index = Arrays.binarySearch(sortedArray, "Birne"); // Result: 1 // Search for Banane: index = Arrays.binarySearch(sortedArray, "Banane"); // Result: -1 // Now with numbers: int[] sortedIntArray = new int[]{1, 3, 4, 6}; // Search for 5 index = Arrays.binarySearch(sortedIntArray, 5); // Result: -3
The result “-3“ means, that the element would be an the 3 place, if it would be there.