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

Answer:

for ( int index=0; index <x.length; index++ )

  if ( x[index] > max )

    max = x[index] ;


Using the Method

call by value
public class ArrayDemo
{
  public static void main ( String[] args ) 
  {
    int[] ar1 =  { -20, 19, 1, 5, -1, 27, 19, 5 } ;
    int biggest = ArrayOps.findMax( ar1 );  // call findMax() with a reference to the array
    System.out.println("The maximum is: " + biggest );
  }
} 

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 ;
  }
}     

The program demonstrates the ArrayOps class.

When you run the program it prints out "The maximum is: 27". You might want to copy this program to a file (call it ArrayDemo.java) and play with it.


QUESTION 4:

During one run of the program, how many arrays are created?


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