go to previous page   go to home page   go to next page

Answer:

The mistake is that zeroAll() does not have a variable j but is attempting to use one. The variable j inside print() cannot be seen by zeroAll().


Array Copy Method

Look back at the previous two pages to see the correct and mistaken version of the program. Here is another version of the program, this time with an (incomplete) method that will copy the values from one array into another.

Both array objects must exist before copy() is called, and both must have the same number of elements. Notice how the declaration for t makes sure that this is so.


// Array Example
//
class ChangeArray
{
  void print ( int[] x )
  {
    for (int j=0; j < x.length; j++)
      System.out.print( x[j] + " " );
    System.out.println( );
  }

  // Copy source to target
  void copy (int[] source, int[] target)
  {
       // more statements here
  }
}

class ChangeTest
{
  public static void main(String[] args)
  {
    ChangeArray cng = new ChangeArray();
    int[] s = {27, 19, 34, 5, 12} ;
    int[] t = new int[ s.length ];
    
    cng.copy( s, t );
    System.out.println( "After copy:" );
    cng.print( t );
  }
}

QUESTION 13:

Fill in the missing code.