/** Array.java - Let's play around with an array. * Let's put the command-line argument values into an array. */ public class Array { public static void main(String [] args) { // use args.length to determine size of array int [] a = new int[args.length]; for (int i = 0; i < a.length; ++i) a[i] = Integer.parseInt(args[i]); int sum = 0; for (int i = 0; i < a.length; ++i) sum += a[i]; System.out.println("The sum is " + sum); // To find the max, first assume that a[0] is the max. // then, loop across the other elements of the array, // to see if any one is larger than max. int max = a[0]; int maxLocation = 0; for (int i = 1; i < a.length; ++i) if (a[i] > max) { max = a[i]; maxLocation = i; } System.out.println("The largest element is " + max + " at index " + maxLocation); // Search the array for the first appearance of a zero. int index = -1; for (int i = 0; i < a.length; ++i) if (a[i] == 0) { index = i; break; } if (index == -1) System.out.println("I could not find a zero in the array."); else System.out.println("Location of first zero is " + index); } }