Individual Rows can be Replaced

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:

myArray[0][0] = 1 ;
myArray[0][1] = 9 ;
myArray[0][2] = 4 ;

Individual Rows can be Replaced

two D array

The following answer will not work:

myArray[0] = {1, 9, 4} ;

An initializer list can only be used to initialize an array, not to assign values to it during a run of a program.

If your answer was:

int[] x = {1, 9, 4} ; // declare and init x

myArray[0] = x ;      // assign to myArray

This will work, but does not quite do what the question asked. The suggested answer puts the values into the required cells of the existing 1D array object of row 0.

The not-quite-right answer replaces the old row 0 by constructing a new 1D array object holding the desired values in its cells, then assigning that object to row 0 of myArray. The previous row 0 is now garbage.

Both answers result in myArray containing what you want. In most programs it will not matter which method you used.

QUESTION 12:

Can row 0 be replaced with an new 1D array that contains a different number of cells than the original row?