// Marty Stepp, CSE 142, Spring 2010 // This program shows methods to swap elements in an array, // and to swap the contents of two entire arrays. // Its purpose is to demonstrate array parameters and reference semantics. import java.util.*; public class SwapAll { public static void main(String[] args) { int[] a1 = {12, 34, 56}; swap(a1, 1, 2); System.out.println(Arrays.toString(a1)); // [12, 56, 34] swap(a1, 0, 2); System.out.println(Arrays.toString(a1)); // swap(a1, 1, 0); System.out.println(Arrays.toString(a1)); // System.out.println(); int[] a3 = {12, 34, 56}; int[] a4 = {20, 50, 80}; swapAll(a3, a4); System.out.println(Arrays.toString(a3)); // [20, 50, 80] System.out.println(Arrays.toString(a4)); // [12, 34, 56] } public static void swapAll(int[] a1, int[] a2) { for (int i = 0; i < a1.length; i++) { int temp = a1[i]; a1[i] = a2[i]; a2[i] = temp; } } public static void swap(int[] a, int index1, int index2) { int temp = a[index1]; a[index1] = a[index2]; a[index2] = temp; } }