Passing Arrays using Methods

From CompSciWiki
Revision as of 21:17, 5 December 2007 by Nick (Talk | contribs)

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 assist in the readability and re-usability of code. At this point you are able to create your own basic methods with singular value variables and not arrays. For code re-usability, you want most to 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 a method.

   

{{{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.

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:
    • Math functions on Arrays on Numbers
    • Sorting Arrays
    • Searching Arrays
    • Printing Arrays
    • The Possibilities and uses are Endless


Cons:

  • At the time of coding, it can seem cumbersome to stop what you are doing and write the header line and comments
  • 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

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: int x;
Array Variable : 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. [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;
}

Methods that return an Array

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 the chapter. But 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.


Section Summary

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
}