Passing Arrays using Methods

From CompSciWiki
Revision as of 11:18, 27 November 2007 by Nick (Talk | contribs)

Jump to: navigation, search

COMP 1010 Home > Parallel Arrays


Introduction

Introduction goes here.(Placeholder)

   

{{{Body}}}

Methods overview

Array Overview

Methods with Arrays as Parameters

Methods that return an Array

Section Summary

Now you can do many different functions using an array. For example you can cacluate the average of an array, and print an array. Now let's look at how you could write a method to perform thoses tasks. Usuallly such methods will accept an array as an arguement.

For example, the below file, SumArray uses a for loop to sum the contents of an array:

public class SumArray
{
    public static void main (String args[]){
        int[] numbers = {1,2,3,4,5,6};
        int sum = 0;

        for (int i = 0; i <numbers.length; i++)
        {
            sum = sum + numbers[i];
        }
        System.out.println(sum);
    }
}

Now, let's imagine that you want to write a method