public class ArrayExamples { public static void printArray(int[] array) { for (int i = 0; i < array.length; ++i) System.out.print(array[i] + " "); System.out.println(); } public static int max(int[] array) { int max = array[0]; for (int i = 1; i < array.length; ++i) if (max < array[i]) max = array[i]; return max; } public static int maxIndex(int[] array) { int maxIndex = 0; for (int i = 1; i < array.length; ++i) if (array[maxIndex] < array[i]) maxIndex = i; return maxIndex; } public static int search(int[] array, int value) { for (int i = 0; i < array.length; ++i) if (array[i] == value) return i; return -1; } public static int binarySearch(int[] array, int value) { int low = 0, high = array.length - 1; while (high >= low) { int mid = (high + low) / 2; if (array[mid] == value) return mid; else if (array[mid] < value) low = mid + 1; else high = mid - 1; } return -1; } public static void main(String[] args) { int numbers1[] = { 10, 5, 7, 2, 9, 12, 11 }; int numbers2[] = { 2, 5, 7, 9, 10, 11, 12 }; System.out.println(max(numbers1)); System.out.println(maxIndex(numbers1)); System.out.println(search(numbers1, 7)); System.out.println(search(numbers1, 8)); System.out.println(search(numbers2, 7)); System.out.println(search(numbers2, 8)); } }