2D Array Declaration

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:

grade 0,0: 99
grade 2,4: 96
sum: 116

2D Array Declaration

Two-dimensional arrays are objects. A variable such as gradeTable is a reference to a 2D array object. The declaration

int[][] myArray ;

says that myArray is expected to hold a reference to a 2D array of int. Without any further initialization, it starts out holding null. The declaration

int[][] myArray = new int[3][5] ;

says that myArray can hold a reference to a 2D array of int, creates an array object of 3 rows and 5 columns, and puts the reference in myArray. All the cells of the array are initialized to zero. The declaration

int[][] myArray = { {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0} };

does exactly the same thing as the previous declaration (and would not ordinarily be used.) The declaration

int[][] myArray = { {8,1,2,2,9}, {1,9,4,0,3}, {0,3,0,0,7} };

creates an array of the same dimensions (same number of rows and columns) as the previous array and initializes the cells to specific values.

QUESTION 7:

After the last statement, what is the value myArray[1][2] ?