Professional Documents
Culture Documents
e.g.
The 2D array created above has 3 rows and two columns. It is represented as
an array of 3 arrays of size 2. Therefore table.length will return 3 (number of
arrays), and table[0].length will return 2 (number of elements in the first
array)
table[0][0] table[0][1]
table[1][0] table[1][1]
table[2][0] table[2][1]
The index of each element has two components: [row number][column number].
Note that, same as 1D array, index starts with 0.
1 3 table
2
3 6 table[0]
4 8 table[1]
5 10 table[2]
6 table[0][0]
7 table[0][1]
8 table[1][0]
9 table[1][1]
10 table[2][0]
11 table[2][1]
Since each element has two indices, in order to loop through each row, and each
column, a two level nested loop is required.
e.g.
Using a 2D array to store a set of five test marks for each of 4 students. Ask the
user to enter each mark and store it in corresponding location.
1
2
3