Difference between revisions of "Passing Arrays using Methods"

From CompSciWiki
Jump to: navigation, search
Line 1: Line 1:
{{Template:1010Topic|Chapter_TOC=[[More With Arrays]]|Introduction=As described in the User defined Methods section (Chapter 7) Methods assist in the redability 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 reusability, you want most to all of your tasks as separate methods, in which case you need to know hopw to pass an array of data to a method to preform some compulation.  In the Following Secition we will discuss the pros and cons to using methods in conjunction to array manipulation.  Then we will go into passing an array to a Method and from a mwthod. |Overview=This section will assist with in the creating of metthods that: <UL><LI>have arrays as parameters</LI></UL>}}
+
{{Template:1010Topic|Chapter_TOC=[[More With Arrays]] > [[Passing Arrays using Methods]]|Introduction=As described in the User defined Methods section (Chapter 7) Methods assist in the redability 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 reusability, you want most to all of your tasks as separate methods, in which case you need to know hopw to pass an array of data to a method to preform some compulation.  In the Following Secition we will discuss the pros and cons to using methods in conjunction to array manipulation.  Then we will go into passing an array to a Method and from a mwthod. |Overview=This section will assist with in the creating of metthods that: <UL><LI>have arrays as parameters</LI></UL>}}
  
 
==Why would you use this?==
 
==Why would you use this?==

Revision as of 14:33, 3 December 2007

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 redability 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 reusability, you want most to all of your tasks as separate methods, in which case you need to know hopw to pass an array of data to a method to preform some compulation. In the Following Secition we will discuss the pros and cons to using methods in conjunction to array manipulation. Then we will go into passing an array to a Method and from a mwthod.

   

{{{Body}}}

Why would you use this?

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