// 11/18/2019 // Wrote several methods to process arrays: // changeArray, find max, find max index, find max of 2 arrays // sum all values in an array, read values into an array import java.util.Scanner; public class ArrraysandMethods { public static void main(String[] args) { int[] numbers = {4, 6, 8, 10, 12}; // some examples of methods and arrays changeArray(numbers); System.out.println(numbers); // this will not print the whole array for (int num : numbers) System.out.println(num); int sum = sumArray(numbers); System.out.println("Sum is: " + sum); int max = maxValue(numbers); System.out.println("Max is: " + max); int numElts = readArray(numbers); for (int i=0; i max) { max = arr[i]; } } return max; } /** maxIndex finds max in arr and returns the INDEX of * the max value */ public static int maxIndex(int[] arr) { int max = arr[0]; int maxIndex = 0; for (int i=1; i max) { max = arr[i]; maxIndex = i; } } return maxIndex; } /** this shows that you no longer need variable max int maxIndex = 0; for (int i=1; i arr[maxIndex]) { maxIndex = i; } } /** return maxIndex; /** maxValue2 finds largest value in 2 arrays */ public static int maxValue2(int[] arr1, int[] arr2) { int max = arr1[0]; for (int i=1; i max) { max = arr1[i]; } } // now max has the max value in arr1 for (int i=0; i max) { max = arr2[i]; } } return max; } /** @return the number of elts read into the array */ public static int readArray(int[] numbers) { Scanner sc = new Scanner(System.in); int counter = 0; System.out.println("Enter a #, ^D to exit:"); while(sc.hasNextInt() && counter < numbers.length) { numbers[counter]=sc.nextInt(); counter++; } return counter; } }