|
| 1 | +package challenge51_60; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * public static int binarySearch(Object[] a, Object key) |
| 7 | + * return the index of the searched key in the given array. |
| 8 | + * otherwise return the -index -1 ; index value is the position where the element should be inserted. |
| 9 | + * the given array should be sorted. |
| 10 | + * the elements in the arrays should be comparable. |
| 11 | + * |
| 12 | + */ |
| 13 | +public class Challenge_56 { |
| 14 | + static String[] marvel = {"Spiderman","Venom","Carnage","Mysterio"}; |
| 15 | + |
| 16 | + public static void main( String[] args ) { |
| 17 | + Arrays.sort(marvel); |
| 18 | + System.out.println(Arrays.binarySearch(marvel, "Xavier")); |
| 19 | + System.out.println(marvel[Arrays.binarySearch(marvel, "Carnage")]); |
| 20 | + System.out.println(Arrays.binarySearch(marvel, "Lizard")); |
| 21 | + System.out.println(Arrays.binarySearch(marvel, "Apocalypse")); |
| 22 | + System.out.println(Arrays.binarySearch(marvel, "Spiderman")); |
| 23 | + } |
| 24 | +} |
0 commit comments