// Helene Martin, CSE 142 // Demonstration of basic array usage import java.util.*; public class ArrayDemo { public static void main(String[] args) { // use the jGRASP debugger to see what these arrays look like int[] values = new int[4]; boolean[] wins = new boolean[67]; String[] names = new String[450]; values[0] = 5; values[1] = 2; values[3] = 6 % 4 + 156; // values[1] is an int so can itself be used as an index values[values[1]] = 15; System.out.println(Arrays.toString(values)); int[] a = {5, 2, 15, 158}; // bad (compares memory locations) System.out.println(values == a); System.out.println(Arrays.equals(values, a)); // array traversal: // loop over each index of the array for (int i = 0; i < values.length; i++) { // do something cool with values[i] System.out.println(values[i]); } // draw out the array and be sure to update values as they change mystery(); } 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; } } // bad! (prints out memory location) System.out.println(a); String aStr = Arrays.toString(a); System.out.println(aStr); } }