COP3337 Section U08 Fall 2015 EXAMPLES OF ARRAYS ================== i. The Program -------------- import java.util.NoSuchElementException; // exaple of arrays public class TestArrays { public static void main(String[] args) { System.out.println("Examples of Arrays"); System.out.println("==================\n\n"); // defining arrays double[] table = { 10.0, 20.5, -15.3, 17.8, 26.1, 5.1, -10.0}; String[] friends = { "Pepe", "Angela", "Diana", "Rich", "Grandpa Smith"}; double[] numbers = new double[0]; short[] smallNumbers = new short[6]; long[] largeNumbers; // show how length works System.out.println("The array table has " + table.length + " numbers."); System.out.println("The array friends has " + friends.length + " names."); // print the friends array printArray("The friends", friends); // test getIndex System.out.println("-15.3 occured in table at index " + getIndex(table, -15.3)); System.out.println("8.5 occured in table at index " + getIndex(table, 8.5)); System.out.println("Print the numbers in table greater than 8 and" + " smaller than 20\n"); printInBetweenItems(table,8,20); System.out.println("The largest item in table is " + getLargest(table)); System.out.println("We try to get the largest number in " + " the array numbers."); try { System.out.println(getLargest(numbers)); } catch (NoSuchElementException e) { System.out.println("We got a no such element exception."); } } // some methods with arrays as parameters // print an array of strings public static void printArray(String arrayName, String[] arr) { // print the header System.out.println(arrayName); // underline it for (int i = 0; i < arrayName.length() ; i++) System.out.print("-"); System.out.println(); // check for empty array if (arr == null) System.out.println("The input array is null."); else if (arr.length == 0) System.out.println("The array is empty."); else // print it using an enhanced for loop { for (String current : arr) System.out.println(current); } System.out.println(); // print a separator line } // return an index at which target occurs in // double array arr // if target does not occur in arr, return -1 public static int getIndex(double[] arr, double target) { final int NOT_FOUND = -1; // check if arr is empty if (arr == null || arr.length == 0) return NOT_FOUND; // search arr for (int i = 0; i < arr.length; i++ ) if (arr[i] == target) return i; // target was not found return NOT_FOUND; } // print all items of arr that are less than high and greater than low public static void printInBetweenItems(double[] arr, double low, double high) { if (arr == null || arr.length == 0) return; // there are no items in arr // print the qualifying numbers using an enhanced for loop for (double item: arr) if (item > low && item < high) System.out.println(item); } // return the largest number in the double array arr public static double getLargest(double[] arr) { // check for empty array if (arr == null || arr.length == 0) throw new NoSuchElementException(); // initialize largest double largest = arr[0]; for (int i = 1; i < arr.length; i++) // check if arr[i] is greater than largest if (arr[i] > largest) largest = arr[i]; return largest; } } ii. The output: --------------- run: Examples of Arrays ================== The array table has 7 numbers. The array friends has 5 names. The friends ----------- Pepe Angela Diana Rich Grandpa Smith -15.3 occured in table at index 2 8.5 occured in table at index -1 Print the numbers in table greater than 8 and smaller than 20 10.0 17.8 The largest item in table is 26.1 We try to get the largest number in the array numbers We got a no such element exception. BUILD SUCCESSFUL (total time: 0 seconds)