for ( int index=0; index <x.length; index++ )
if ( x[index] > max )
max = x[index] ;
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.
ArrayDemo
. ArrayDemo
holds the static main()
method.main()
creates an array object referenced by ar1
findMax()
method of the ArrayOps
class is called
with a reference to the array object as an actual parameter.
findMax()
uses the reference to access the arrayfindMax()
scans the array for the maximumWhen 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.
During one run of the program, how many arrays are created?