Passing Arrays as Parameters

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > More With Arrays > Passing Arrays as Parameters


Introduction

As described in the User Defined Methods section (Chapter 7), primitive data types can be passed to methods as parameters. This section will describe how to pass arrays as parameters and the difference between arrays and a primitive data types when being passed to methods.


Passing a Primitive Data Type

Passing arrays to methods is similar to passing primitive data types. To read up on passing primitive data types see Passing Arguments using Methods


Passing an Array

Method Headers

To pass an array of variables it is identical to passing a single variable with the exception of placing a "[]" between your Variable type and your variable name like so:

 Single Variable: public int add(int x, int y){}
Array Data Structure : public int add(int[] x, int[] y){} 

The following is a method named add that passes an array as a parameter, calculates the sum of the array elements and returns the sum.

 public int add(int[] x)
{
    int sum = 0;
    for(int i = 0; i < x.length; i++)
        sum = sum + x[i];
    
    return sum;
} 

As you can see the for loop uses x.length. This gives you the amount of elements in the array.

makeString method:

 public String makeString(char[] word)
{
    String myString = "";
    for(int i = 0; i < word.length; i++)
        myString = myString + word[i];

    return myString;
} 

Method Call

Calling the method is exactly the same as if you were sending a single value variable.

 // Single Variable:
    int x = 1;
    int y = 2;
    int sum = add(x,y);

// Array Data Structure:
    int[] x = {1, 2);
    int sum = add(x); 


Returning an Array

Method Headers

Having a method return an array is very similar to returning a primitive data type. Lets say you were given a String and you were asked to return an array of characters. The method would look something like this:

 public char[] destroyString(String word)
{
    char[] charWord = new char[word.length()]; //creates an array of chars named "charWord" with the same size as the String "word"
    for(int i = 0; i < word.length(); i++)
        charWord[i] = word.chatAt(i);

    return charWord;
} 

Notice the return type "char[]" in the method header. This tells the method you will be returning an array of chars.

Method Call

Here is how you make a call to destroyString and store the char array it returns.

 String word = "Hello World!";
    char[] charWord = new char[word.length()];
    charWord = makeString(word); 


The Difference between Arrays and Primitive Data when used as Parameters

As you can see, arrays act very similar to primitive data types when being used in methods. However, there is one key difference that needs to be kept in mind. When passing a primitive data type as a parameter, you are passing a copy of the data. Therefore, changes made to the data are lost when the method returns unless the data is returned. When passing an array as a parameter you are passing a pointer to the arrays memory location. Therefore, changes made to the array in the method are kept even when the method returns.


Section Summary

Arrays are a very useful data structure and passing them to methods will allow you to manipulate their data in a clean way. You now know how to pass them to and return them from a method. They act very similar to primitive data types but how they are passed as parameters lead to their key difference.

Previous Page: More With Arrays Next Page: Partially Filled Arrays