// CSE 142 Lecture 17 // Arrays for Tallying // This program demonstrates array reference semantics import java.util.*; // For Arrays public class ArraySwap { public static void main(String[] args) { int[] a = {1, 2, 3}; int[] b = {4, 5, 6}; System.out.println("a: " + Arrays.toString(a) + ", b: " + Arrays.toString(b)); swapAll(a, b); System.out.println("a: " + Arrays.toString(a) + ", b: " + Arrays.toString(b)); } // Swap the contents of two arrays. // Why doesn't the following code work? // int[] temp = first; // first = second; // second = temp; // Can we write this method for arrays with different lengths? public static void swapAll(int[] first, int[] second) { for (int i = 0; i < first.length; i++) { int temp = first[i]; first[i] = second[i]; second[i] = temp; } } }