go to previous page   go to home page   go to next page

Answer:

Yes. New lines are inserted into the program to do this:


Formal Parameter Connected to New Data

public class ArrayDemo
{
  public static void main ( String[] args ) 
  {
    int[] ar1 =  { -20, 19, 1, 5, -1, 27, 19, 5 } ;
    int[] ar2 =  { 2, 4, 1, 2, 6, 3, 6, 9 } ;

    System.out.println("The first  maximum is: " + ArrayOps.findMax( ar1 )  );    
    System.out.println("The second maximum is: " + ArrayOps.findMax( ar2 )  ); 
  }
} 

class ArrayOps
{                                        // the parameter x will contain the array reference
  public static int findMax( int[] x )   // this method is called with.                     
  {
    int max = x[0];
    for ( int index=0; index < x.length; index++ )
      if ( x[index] < max )
        max = x[index] ;

    return max ;
  }
}     

In the revised program, the findMax() method is used first with one array, and then with the other array. This is possible because the parameter x of the method refers to the current array, whichever one is used in the method call.

The program prints:

C:\>java ArrayDemo
The first maximum is: 27
The second maximum is: 9

QUESTION 6:

  1. Must each array contain the same number of elements?
  2. Must each array be an array of int?

go to previous page   go to home page   go to next page