public class Main {
// swap1, does work
public static void swap1(int[] a1, int[] a2)
{
for (int i = 0; i < a1.length; i++)
{
int arrhold = a1[i];
a1[i] = a2[i];
a2[i] = arrhold;
}
}
// swap2, does not work
public static void swap2(int[] a1, int[] a2)
{
int[] arrhold = a1;
a1 = a2;
a2 = arrhold;
}
public static void main(String[] args) {
// Harbor, Week 1, Mock 1, PR_PracticeTest_1.pdf
// AP CS A
// MCQ 26
int arr1[] = {1, 2, 3};
int arr2[] = {7, 8, 9};
for (int item: arr1)
{
System.out.print(item);
System.out.print(" ");
}
System.out.println();
for (int item: arr2)
{
System.out.print(item);
System.out.print(" ");
}
System.out.println();
System.out.println("arr1 and arr2 before swap1(arr1, arr2) \n");
swap1(arr1, arr2);
System.out.println("swap1(arr1, arr2) does work.");
for (int item: arr1)
{
System.out.print(item);
System.out.print(" ");
}
System.out.println();
for (int item: arr2)
{
System.out.print(item);
System.out.print(" ");
}
System.out.println("\n");
swap2(arr1, arr2);
System.out.println("swap1(arr1, arr2) does nothing.");
for (int item: arr1)
{
System.out.print(item);
System.out.print(" ");
}
System.out.println();
for (int item: arr2)
{
System.out.print(item);
System.out.print(" ");
}
System.out.println("\n");
}
}