2D Arrays in Java

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:

gradeTable[ 0 ][ 0 ] 99
gradeTable[ 1 ][ 1 ] 91
gradeTable[ 3 ][ 4 ] 93
gradeTable[ 5 ][ 2 ] 92

2D Arrays in Java

StudentWeek
01234
0 99 42 74 83 100
1 90 91 72 88 95
2 88 61 74 89 96
3 61 89 82 98 93
4 93 73 75 78 99
5 50 65 92 87 94
6 43 98 78 56 99
gradeTable

In Java, a table may be implemented as a 2D array. Each cell of the array is a variable that can hold a value and works like any variable. As with one dimensional arrays, every cell in a 2D array is of the same type. The type can be a primitive type or an object reference type.

Important: Each cell of the array is specified with a row and column number, in that order.

Say that gradeTable is a 2D array of int, and that (conceptually, at least) it looks like the table to the right. Then,

gradeTable[ 0 ][ 1 ]     // holds 42
gradeTable[ 3 ][ 4 ]     // holds 93
gradeTable[ 6 ][ 2 ]     // holds 78

The subscripted variables are used in assignment statements and arithmetic expressions just like any variable:

gradeTable[ 0 ][ 1 ] = 33 ;   // puts a 33 into row 0 column 1. 
gradeTable[ 3 ][ 4 ]++ ;      // increments the value at row 3 column 4. 
int value = (gradeTable[ 6 ][ 2 ] + 2) / 2 ;  // puts 40 into value 

QUESTION 3:

Write a Java statement that puts a zero into row 5 column 3.