Passing Arrays using Methods

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > More With Arrays > Passing Arrays using Methods


Introduction

As described in the User Defined Methods section (Chapter 7), methods aid in the readability and re-usability of code. At this point you are able to create your own basic methods with single value variables. For code re-usability, you want all of your tasks as separate methods, in which case from time to time you will encounter an issue where you you need to know how to pass an array of data to a method for computation.

In the following section we will discuss the pros and cons to using methods in conjunction with array manipulation. Then we will go into passing an array to and from methods.

   

{{{Body}}}

Why would you use this?

Let's say you have an array full of data that needs to be manipulated. You can write a loop in the current method to manipulate the data at that point in the code. What happens if this manipulation needs to be done several times or different parts of your program will need to do the same computation? Instead of having to re-write that loop/procedure whenever you need to manipulate the array, write a method that can be called when needed.

Methods using Arrays - Pros:

  • Re-usable Code, only have to write it once
  • Code is easier to read and follow
  • If there is a bug in the Method you only have to fix it in one place.

  • Great uses for Methods that use Arrays:


Methods using Arrays -Cons:

  • At the time of coding, it can seem cumbersome to stop what you are doing and write the header line and comments for the method
  • If you do not have the correct parameters and not exactly how to pass the variables it can get frustrating


Methods with Arrays as Parameters

Method Headers

Passing arrays to methods are not much different than passing a typical variable. The following is an example of single variables passed to a method with a single integer being returned.

public int add(int x, int y)
{
    int sum = x + y;
    return sum;
}

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 Variable : public int add(int[] x){}

The following is the same example as the single variable adding method, but instead of sending 2 single variables, I am sending the method an array of integers.

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

Note: in Java you do not have to pass the length of the array to the method. Java has a built in property that allows you to find the length of an array on the fly.
   For arrays use: variable.length
   For classes like Strings use: variable.length()


Another example:

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 Variable:
    int[] x = {1, 2);
    int sum = add(x);


Methods that return an Array

Method Headers

As indicated above, having a method that returns an array instead of a single variable is not that different. Lets re-visit the makeString method described in the Methods with Arrays as Parameters Section of this chapter. 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[] makeString(String word)
{
    char[] charWord = new char[word.length()]; //creating an array that has a size of the length of the initial string.
    for(int i = 0; i < word.length(); i++)
        charWord[i] = word.chatAt(i);

    return charWord;
}

As you can see, it is no different then if you were passing the array as a parameter than if you were using it as a return value. The concept is the same.

Method Call

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

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

Array Variable:
    String word = "my words";
    char[] charWord = new char[word.length()];
    charWord = makeString(word);


Methods that use Arrays as Parameters and Return Values

Now that we have covered passing an array as a parameter and returning an array from a method, combining the two should be an easy task. Here is an example of a method header that would use both passing and returning arrays from a method.

public int[] sortIntList(int[] myList)
{
    //sort the list
    //return the sorted array of integers
}


Section Summary

As you can see, using arrays in methods are not overly difficult. These methods are not hard to construct, and will save you a lot of time and effort when writing your programs. Methods are always the better way to go when writing a program for re-usability of code and readability. So don;t let arrays scare you off of using methods, they are your friend.