Using arrays with methods

 

Printing out the contents of an array is such a common operation that it would be meaningful to create a method for it.  Such a method would take an array as a parameter – that is the array whose values we’d like to print out.   To specify an array as a parameter, we simply put its declaration statement in the method header.  The code for printing out the elements is no different from before.  Here’s the method implementation:

 

            public static void printArray(int[] array)

      {

            for (int i = 0; i < array.length; i++)

                  System.out.print(array[i] + “ “);

      }

 

To test this method, we would create an array of integers in main (call it numbers), invoke printArray, passing numbers as an argument.  Here’s the code for main (the code for both main and printArray would need to be in the same class):

 

      public static void main(String[] args)

      {

            int[] numbers = {77, 44, 22, -33, 99};

 

            System.out.print(“The numbers are: “);

            printArray(numbers);

      }

 

Notice that when an array is an argument, only the variable name is specified, in this case numbers.  This is no different than passing an integer or String as an argument to a method.   The output of the program will be: 

 

      The numbers are: 77 44 22 -33 99

 

There’s a critical difference, however, between passing an array and passing a variable of a simple type (such as int) as parameter.  In the case of an array, the values of the argument are not copied to the parameter.  Instead, both the argument variable and parameter refer to the same array.  An array is said to be passed by reference, whereas simple data types (int, double, etc.) are passed by value (the value of the argument is copied to the parameter). 

 

      numbers     (the array is referenced by ‘numbers’ in main)

 

 


77

44

22

-33

99

                    0          1           2           3            4                       

 

 

      array    (the same array is referenced by ‘array’ in printArray)

 

In effect, an array variable in the parameter list is an alias to the same array that is referenced by the argument. 

 

To illustrate the difference between pass by value and pass by reference, suppose we have a method that accepts two parameters, an array and an integer.  The method adds one to each array element and to the other parameter.  The method uses a loop to iterate through the array elements, incrementing each by one, and then simply adds one to the integer parameter.  The method does not return anything (a void).  Here’s the code:

 

            public static void addOne(int[] array, int num)

      {

            // increment each array element by one

            for (int i = 0; i < array.length; i++)

                  array[i]++;

 

            num++;      // add one to the parameter

      }

 

Now consider the following main, which declares an array and an integer and invokes addOne to see whether the changes that the method makes are reflected in the arguments (the code assumes that printArray is available for us to invoke).

 

            public static void main(String[] args)

      {

            int[] myArray = {1, 1, 1};

            int num = 10;

 

            System.out.println(“The value of num before addOne is: “ + num);

            System.out.println(“Array values before addOne: “);

            printArray(myArray);

 

            addOne(myArray, num);

 

            System.out.println(“Array values after addOne: “);

            printArray(myArray);

            System.out.println(“The value of num after addOne is: “ + num);

      }

           

The output of this code is:

 

      The value of num before addOne is: 10

      Array values before addOne: 1 1 1

      The value of num after addOne is: 10

      Array values after addOne: 2 2 2

 

The critical difference here is that even though the integer parameter and argument are named the same (num), they are two distinct variables.  It is the value of num inside addOne that is changed, not the argument.  On the other hand, myArray and array reference the same collection of integers, so when addOne modifies array, the changes will be reflected in main via the argument myArray.