/* * ArrayParametersDemo.java * * This program demonstrates some of the consequences of the fact that arrays * are passed by reference. * * Copyright (c) 2000 - Russell C. Bjork * */ public class ArrayParametersDemo { public static void main(String [] args) { int v = 42; int [] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int [] array2 = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; System.out.println("Before call to foo()"); System.out.println("v = " + v); printArray("array1", array1); printArray("array2", array2); foo(v, array1, array2); System.out.println("After call to foo()"); System.out.println("v = " + v); printArray("array1", array1); printArray("array2", array2); } private static void printArray(String label, int [] array) { System.out.print(label + " = "); for (int i = 0; i < array.length; i ++) System.out.print(array[i] + " "); System.out.println(); } private static void foo(int x, int [] a1, int [] a2) { int [] a3 = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }; x ++; a1 = a3; for (int i = 0; i < a2.length; i ++) a2[i] *= 2; } }