Java Reference
In-Depth Information
The following program illustrates how arrays are passed as actual parameters in a
method call.
EXAMPLE 9-6
// This program illustrates how arrays are passed as parameters
// to methods.
import java.util.*;
//Line 1
public class ArraysAsParameters
//Line 2
{
//Line 3
static final int ARRAY_SIZE = 10;
//Line 4
public static void main(String[] args)
//Line 5
{
//Line 6
int [] listA = new int [ARRAY_SIZE];
//Line 7
int [] listB = new int [ARRAY_SIZE];
//Line 8
System.out.print("Line 9: listA elements: ");
//Line 9
//output the elements of listA using
//the method printArray
OneDimArrayMethods.printArray(listA,
listA.length);
//Line 10
System.out.println();
//Line 11
System.out.print("Line 12: Enter " + listA.length
+ " integers: ");
//Line 12
//input data into listA using the method fillArray
OneDimArrayMethods.fillArray(listA,
listA.length);
//Line 13
System.out.println();
//Line 14
System.out.print("Line 15: After filling "
+ "listA, the elements are:"
+ "\n
");
//Line 15
//output the elements of listA
OneDimArrayMethods.printArray(listA,
listA.length);
//Line 16
System.out.println();
//Line 17
//find and output the sum of the elements of listA
System.out.println("Line 18: The sum of the "
+ "elements of listA is: "
+ OneDimArrayMethods.sumArray(listA,
listA.length));
//Line 18
//find and output the position of the (first)
//largest element in listA
Search WWH ::




Custom Search