| 9 | |
| 10 | public class ArraySearching { |
| 11 | public static void main(String[] args) { |
| 12 | Rand.Pint rand = new Rand.Pint(); |
| 13 | int[] a = new Rand.Pint().array(25); |
| 14 | Arrays.sort(a); |
| 15 | show("Sorted array", a); |
| 16 | while(true) { |
| 17 | int r = rand.getAsInt(); |
| 18 | int location = Arrays.binarySearch(a, r); |
| 19 | if(location >= 0) { |
| 20 | System.out.println( |
| 21 | "Location of " + r + " is " + location + |
| 22 | ", a[" + location + "] is " + a[location]); |
| 23 | break; // Out of while loop |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | /* Output: |
| 29 | Sorted array: [125, 267, 635, 650, 1131, 1506, 1634, |