Yes.
class ArrayOps
{
// other methods
// print elements start through end
//
static 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 )
{
int[] ar1 = { -20, 19, 1, 5, -1, 27, 19, 5 } ;
// print elements at indexes 1, 2, 3, 4, 5
ArrayOps.printRange( ar1, 1, 5 );
}
}
Often parameters are used to fine-tune the actions
performed by a method.
For example,
imagine that you want to print only some of the
elements of an array.
Above is the ArrayOps
class definition with
a new method added.
The method printRange()
prints the elements
from start
to (and including) end
.
In this particular program,
main()
uses the method to print the elements
at indexes
1 through 5.
The values 19, 1, 5, -1, 27 are printed.
Fill in the blanks for printRange()
.
Assume that start
and end
are legal indexes for the array.