// Helene Martin, CSE 142 // Demonstrates array usage. import java.util.*; public class ArrayDemo { public static void main(String[] args) { int[] scores = new int[10]; scores[0] = 34; scores[1] = 56 % 3 + 2; //int x = scores[1]; scores[scores[1]] = 5; // scores[4390284] = 3; ArrayIndexOutOfBoundsException System.out.println("Value at index 4: " + scores[4]); double[] temps = new double[3]; // use array traversals // note that length is a field, not a method as for Strings for (int i = 0; i < temps.length; i++) { temps[i] = 12 + 2 * i; } System.out.println(temps); // no good!! double[] temps2 = {45, 23.4, 44.3, 17}; // quick initialization syntax // Use jGRASP's debugger to see what these look like boolean[] wins = new boolean[7]; // default: false String[] names = new String[9]; // default: null (no reference) names[0] = "Sally"; // sets the first reference Scanner[] scanners = new Scanner[10]; // default: null (for all object types) } public static void mystery() { int[] a = {1, 7, 5, 6, 4, 14, 11}; for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { a[i + 1] = a[i + 1] * 2; } } } }