public class PrintArr { // this is just a SAMPLE main() -- the point of this example is really // the printArray method public static void main(String[] args) { int[] list1 = {2, 4, 6, 8, 10}; int[] list2 = new int[20]; for (int i = 0; i < list2.length; i++) list2[i] = i * 3; System.out.println("The contents of list1: "); printArray(list1); System.out.println("The contents of list2: "); printArray(list2); double ans1 = average(list1); System.out.println("The average of list1 is: " + ans1); System.out.println("The average of list2 is: " + average(list2)); int[] list3 = {-1, -4, 15, 9, -5, -2}; System.out.println("The contents of list3: "); printArray(list3); int[] list4 = zeroNegArray(list3); System.out.println("The contents of list4: "); printArray(list4); System.out.println("The contents of list3: "); printArray(list3); } // method to print any integer array public static void printArray(int[] arr) { System.out.print("{"); int i; for (i = 0; i < arr.length-1; i++) System.out.print(arr[i] + ", "); System.out.println(arr[i] + "}"); } public static double average(int[] arr) { int sum = 0; for ( int i = 0; i < arr.length; i++) sum = sum + arr[i]; return (double)sum / arr.length; } // takes in an integer array, and returns an array that zeroes // out any negative numbers in the original // NOTE: This function does NOT change the original!!!!!!! public static int[] zeroNegArray(int[] arr) { int[] newarray = new int[arr.length]; // build new array // same size as arr for (int i = 0; i < arr.length; i++) { if( arr[i] < 0 ) newarray[i] = 0; else newarray[i] = arr[i]; } return newarray; } }