next up previous contents
Next: 7 Numerical problems and Up: 6 Multiple methods Previous: Class variables, method variables   Contents

Passing arrays to methods

You will remember from the section about arrays, that arrays are reference variable types and as such their behaviour is slightly different from that of variables of primitive data types such as double, int, etc. Passing arrays to or from methods can be very useful. However it is the main area where reference data types behave slightly differently from primitives.

To pass an array to a method, the method declaration should look like this:

    public static void takeArrayMethod(double[] numberList)
    {
        // method code
        return;
    }

and then the method call might look like:

    takeArrayMethod(vector1);                     //or
    takeArrayMethod({1.0, 3.4, 5.2});

where vector1 would be an array of double values already created by the program.

For a method to return an array the method should be declared as you might expect:

    public static int[] returnArrayMethod()
    {
        // method code which includes creating an int array called vector2
        return vector2;
    }

When a primitive data type, e.g. double, is passed to a method, its value is copied to the new method variable. For reference data types, a new reference is created, but unlike for primitives, the data that is referenced is not copied to a new area of memory. Instead the new reference is set to refer to the original area of memory storing the data. This is known as passing by reference.

As was mentioned earlier it is not possible for a method to change the value of its arguments. E.g.

    // method that tries to change argument value
    public static double increase(double x)
    {
        x += 10;                             // code not allowed so
        return x;                            //    will not compile
    }

The equivalent for an array is that the variable (i.e. the array name) can not have its reference changed. E.g.

    // method that tries to change argument reference
    public static int[] change(int[] vector2)
    {
        vector3 = {1,2,3};
        vector2 = vector3                    // code not allowed so
        return vector3;                      //    will not compile
    }

However it is possible to change individual values in the array, in the area of memory that vector2 refers to. This is in contrast to primitive data types where the value of an argument can not be changed. E.g.

    // method that changes the values of an array
    public static void valueChange(int[] vector2)
    {
        for(int i=0; i<vector2.length; i++)
        {
            vector2[i] = i+1;                // this IS allowed
        }
        return;
    }
Note that in this particular case, since any value changes are made to data in the original area of memory, there is no need to have return vector2; thus the method is declared as void.

Exercise 6.4


(5 marks)



It is useful to have generic methods to perform common tasks that you will use again and again, saving yourself a lot of typing. It also makes your code more concise, abstract and easy to understand. One common programming task is to print the contents of an array. In a large program with arrays this might have to be done many times. For this exercise you will write a method to do this and use it to simplify some code you wrote in a previous exercise.

If you need to print arrays in any future exercises, you should try to re-use this method.




next up previous contents
Next: 7 Numerical problems and Up: 6 Multiple methods Previous: Class variables, method variables   Contents
RHUL Dept. of Physics