for ( int index=0; index <x.length; index++ )
if ( x[index] > max )
max = x[index] ;
Here is how our new method is used by a main()
program.
Notice that an ArrayOps
object must be created before
the method can be used.
class ArrayDemo { public static void main ( String[] args ) { int[] ar1 = { -20, 19, 1, 5, -1, 27, 19, 5 } ; ArrayOps operate = new ArrayOps(); // create an ArrayOps object int biggest = operate.findMax( ar1 ); // call findMax() with a reference to the array System.out.println("The maximum is: " + biggest ); } }
The program defines a class called ArrayDemo
.
ArrayDemo
holds the static main()
method.main()
creates an array object, ar1
main()
creates an ArrayOps
object, operate
.findMax()
method of the ArrayOps
object is called
with a reference to the array object ar1
as a parameter.
What, exactly, is ar1
?