Yes.
Often parameters are used to "fine-tune" the actions
performed by a method.
For example,
imagine that you want to print out only some of the
elements of an array.
Here is the ArrayOps
class definition with
a new method added:
import java.io.*; class ArrayOps { void print( int[] x ) { for ( int index=0; index < x.length; index++ ) System.out.print( x[index] + " " ); System.out.println(); } // print elements start through end void printRange ( int[] x, int start, int end ) { for ( int _________ ; ____________; __________ ) System.out.print( x[index] + " " ); System.out.println(); } } class ArrayDemo { public static void main ( String[] args ) { ArrayOps operate = new ArrayOps(); int[] ar1 = { -20, 19, 1, 5, -1, 27, 19, 5 } ; // print elements 1, 2, 3, 4, 5 operate.printRange( ar1, 1, 5 ); } }
The new method printRange()
is to print out the elements
from start
to end
(inclusive.)
For example,
the main()
method asks to print out the elements from
1 to 5 (inclusive).
The values 19, 1, 5, -1, 27 will be printed.