Array Example

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

No effect at all. Other than for illustrating how Java works, this is not a very useful program.

Array Example

Here is another example program, this time using an array parameter.

The details of the program will be explained in a few pages. For now, just look at the broad outline of the program. What are the methods of a ChangeArray object? What do they do?

// 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 prints:

Before:
27 19 34 5 12
After:
0 19 34 5 12

QUESTION 6:

Did the zeroElt() method change one of the array's elements?