import java.util.*; public class ArrayExamples { public static void main(String[] args) { mystery(); System.out.println(); // We saw that we could pass arrays as parameters. int[] evens = {2, 4, 6, 8}; sumEvens(evens); // value semantics vs. reference semantics // primitive vs. arrays, other Objects // value semantics: int x = 7; int y = 14; int z = x; // note that z and x are not connected! z gets a copy of // whatever x was. So changes to one variable do not affect // the other. System.out.println("BEFORE x: " + x + " y: " + y + " z: " + z); swap(x, y); System.out.println("AFTER x: " + x + " y: " + y + " z: " + z); System.out.println(); // reference semantics: int[] list = {1, 2, 3}; int[] list2 = list; // both list and list2 store a reference to the same // array, so changing either list or list2 changes // the array. System.out.println("BEFORE list = " + Arrays.toString(list)); modify(list); System.out.println("AFTER list = " + Arrays.toString(list)); } public static void modify(int[] list) { list[0] = list[list.length - 1]; } public static void swap(int a, int b) { // a = 7, b = 14 int temp = a; a = b; b = temp; // a = 14; b = 7 } public static void sumEvens(int[] evens) { int sum = 0; for (int i = 0; i < evens.length; i++) { sum += evens[i]; } System.out.println("sum: " + sum); System.out.println(); } public static void mystery() { int[] a = {1, 7, 5, 6, 4, 14, 11}; // for (int i = 0; i < a.length; i++ // standard loop traversal for (int i = 0; i < a.length - 1; i ++) { if (a[i] > a[i + 1]) { a[i + 1] = a[i + 1] * 2; } } System.out.println("mystery array = " + Arrays.toString(a)); } }