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


// Same as Before
class ArrayOps
{
  int findMax( int[] x )   // x points to an array object                    
  {
    int max = x[0];
    for ( int index=0; index <x.length; index++ )
      if ( x[index] > max )
        max = x[index] ;
    return max ;
  }
}

class ArrayDemo
{
  public static void main ( String[] args ) 
  {
    ArrayOps operate = new ArrayOps();
    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: " + operate.findMax( ar1 )  );
    
    System.out.println("The second maximum is: " + operate.findMax( ar2 )  );    
  }
}      

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 7:

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