// Array Example
//
class ChangeArray
{
void print ( int[] x )
{
for ( int j=0; j < x.length; j++ )
System.out.print( x[j] + " " );
System.out.println( );
}
void zeroElt ( int[] x, int elt ) // 6.
{
if ( elt < x.length ) // 7.
x[ elt ] = 0; // 8.
}
}
class ChangeTest
{
public static void main ( String[] args ) // 1.
{
ChangeArray cng = new ChangeArray(); // 2.
int[] value = {27, 19, 34, 5, 12} ; // 3.
System.out.println( "Before:" ); // 4.
cng.print( value );
cng.zeroElt( value, 0 ); // 5.
System.out.println( "After:" ); // 9.
cng.print( value );
}
}
|
- The program starts running with the static main method.
- A ChangeArray object is constructed.
- The int[] object value is constructed and initialized.
- The numbers currently held in value are printed out.
- Before: 27 19 34 5 12 is printed
- The zeroElt() method is called with value and 0 as parameters.
- A reference to the array value is copied
into the parameter x, and
0 is copied into the parameter elt.
- The zero() method checks if the requested element (0) exists
- Element 0 of the array is set to zero.
- The numbers currently held in value are printed out.
- After: 0 19 34 5 12 is printed
|