Arrays and Functions

Arrays are objects, which means that when we declare an array, the name of the array is a reference. When we do a new for the array, Java creates the array and puts the address of the array into our variable.

This means that arrays behave differently when passed as parms. When a primitive variable is passed to a method, the value of the variable is unchanged after the call, even if the method modifies the variable. This is because when the parm is passed, the actual is a copy of the formal.

When an array is passed as a parm, the array name (the reference) is passed. This reference points to the array, as shown in the picture above. The actual parm is a copy of the array name. But this copy still points to the original array elements. This means that the if the method modifies the array elements, the changes will be available in the calling method.

Passing an Array

In the call: just use the array name, without any brackets. For example: arrfunc(thearray);

In the method header: the parm type must have brackets, but do not put brackets after the array name. The length field for the array is available in the called method. For example: public static void arrfunc(int[] arr);

Method With Array Parameter

     import java.util.Scanner;
     public class ArParms1 {
        public static void main(String [] args) {
           final int SIZE = 5;
           int [] numbers = new int[SIZE];
           int i = 0;

           readArray(numbers);
           for (i = 0 ; i < numbers.length ; i++) {
              System.out.print(numbers[i] + " ");
           }
           System.out.println();
        }

        public static void readArray(int[] numbers) {
           int i;
           System.out.println("Enter " + numbers.length + " numbers: ");
           Scanner keybd = new Scanner(System.in);
           for (i = 0 ; i < numbers.length ; i++)
              numbers[i] = keybd.nextInt();
        }
     }

Passing an Array That Is Partially Used

If you will use only part of the array, you must pass another parm for the size of the array being used. The following example reads till a sentinel is entered, so only part of the array is used. A variable must be used to keep track of the part of the array used, so that loops can stop at that point and not process the unused array locations.

     import java.util.Scanner;
     public class ArParms2 {
        public static void main(String [] args) {
           final int SIZE = 25;
           int [] numbers = new int[SIZE];
           int i = 0;
           int size;

           size = readArray(numbers);
           printArray(numbers, size);
        }

        public static int readArray(int[] numbers) {
           int i = 0;
           System.out.println("Enter at most " + numbers.length + " numbers (-999 to end): ");
           Scanner keybd = new Scanner(System.in);
           numbers[0] = keybd.nextInt();
           while (numbers[i] != -999) {
              i++;
              numbers[i] = keybd.nextInt();
           }
           return i;
        }

        public static void printArray(int[] numbers, int size) {
           int i = 0;
           for (i = 0 ; i < size ; i++)
              System.out.print(numbers[i] + " ");
		   System.out.println();
        }
     }

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2013