KwickAcademy Java · 6 min · free
Two-dimensional Arrays and Matrices
How a 2D array stores rows and columns, m.length versus m[0].length, nested loops for input and display, row and column sums, diagonals and transpose.
Follows the syllabus of: ICSE Class 10 Computer Applications, Cambridge IGCSE Grade 9 Computer Science (0478), Cambridge IGCSE Grade 10 Computer Science (0478)
On screen in this lesson
What is a 2D array?
| Values arranged in rows and columns, like a table |
| Also called a matrix |
| Each element needs two indexes: row, then column |
| Both indexes start at 0 |
Rows and columns
| Column 0 | Column 1 | Column 2 |
|---|---|---|
| Row 0: 1 | 2 | 3 |
| Row 1: 4 | 5 | 6 |
| Row 2: 7 | 8 | 9 |
The two diagonals
| Diagonal | Rule | Cells in 3 × 3 |
|---|---|---|
| Left (main) | row == column | (0,0) (1,1) (2,2) |
| Right | row + col = n - 1 | (0,2) (1,1) (2,0) |
Quick recap
| A 2D array stores rows and columns: m[row][col] |
| m.length is rows; m[0].length is columns |
| Nested loops input and display a matrix |
| Left diagonal: row == col; right: row + col = n - 1 |
| Transpose swaps rows and columns |
Quick answers
What is the difference between m.length and m[0].length?
m.length gives the number of rows; m[0].length gives the number of columns.
Where is the right diagonal?
Where row + column equals n - 1.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
Why does a 2D array need two indexes?44 sec
Why does my matrix print on one long line?41 sec
What changes between row sums and column sums?43 sec
What is a transpose?38 secThe full lesson, in text
Hello students, welcome to Kwickprep. In a cinema hall, your ticket says row C, seat 7. One number is not enough, you need two. Today we learn two-dimensional arrays, and the matrix programs boards ask: row sums, column sums, diagonals and transpose.
A two-dimensional array stores values in a grid. The values are arranged in rows and columns, like a table or a class timetable. In maths, such a grid is called a matrix. To reach one element, we need two indexes, first the row and then the column. As with every Java array, both indexes start at zero.
Picture a matrix M with three rows and three columns. Row zero holds one, two and three. Row one holds four, five and six. So M of one, two, meaning row one and column two, is six. Row two holds seven, eight and nine.
In Java, two pairs of square brackets make a two-dimensional array. Each inner pair of curly brackets is one row, so this matrix has two rows and three columns. M of one, two is row one, column two, which is six. M dot length gives the number of rows, which is two. M of zero dot length gives the number of columns in row zero, which is three. To create an empty one, write new int, three, three, for three rows and three columns.
To fill a matrix from the keyboard, we need two loops, one inside the other. This is called a nested loop. R is the row counter, and C is the column counter. For each row, the inner loop visits every column, and next int reads one value into that box. So the user types nine numbers, row by row.
To show a matrix as a grid, we again use a nested loop. The inner loop uses print, not println, so the values of one row stay on the same line. A space is added after each value. After each row, an empty println moves to a new line. Pause and predict. What if we remove that empty println? All six numbers would print on one line.
Now the calculations. To find the sum of each row, the row loop stays outside. S, the sum, is reset to zero at the start of every row. The inner loop adds all the values in that row. Row zero gives one plus two plus three, which is six. Row one gives four plus five plus six, which is fifteen.
For column sums, we simply swap the loops. Now the column loop is outside, and the row loop is inside. Column zero gives one plus four, which is five. Column one gives two plus five, which is seven. Column two gives three plus six, which is nine.
A square matrix has the same number of rows and columns, and it has two diagonals. On the left diagonal, also called the main diagonal, the row and column numbers are equal. On the right diagonal, row plus column equals N minus one, where N is the size. For a three by three matrix, that sum is always two.
Diagonals need only one loop, because one counter gives both indexes. D one adds the left diagonal, where the row and the column are both the counter. D two adds the right diagonal, where the column is two minus the counter. The left diagonal is one plus five plus seven, which is thirteen. The right diagonal is three plus five plus nine, which is seventeen.
The transpose of a matrix turns its rows into columns. A matrix of two rows and three columns becomes three rows and two columns. In code, we print with the column loop outside and the row loop inside. So the first column, one and four, now prints as the first row. In general, transpose element T of R, C equals M of C, R.
Another common program adds two matrices P and Q of the same size. We create a new matrix S for the sum. The nested loops visit every position. Each element of S is the element of P plus the element of Q at the same row and column. Both matrices must have the same number of rows and columns.
Some exams ask for two-dimensional arrays in pseudocode. Here M is declared with rows one to three and columns one to four. A comma separates the row and column inside one pair of brackets. The nested loops set every element to zero.
Let us revise. A two-dimensional array stores values in rows and columns, and we write the row first. M dot length gives the rows, and M of zero dot length gives the columns. Nested loops are used to input and display a matrix. On the left diagonal row equals column, and on the right diagonal row plus column is N minus one. And transpose turns rows into columns. Practise each program with a three by three matrix.
Courses that teach this
| Course | Unit |
|---|---|
| ICSE Class 10 Computer Applications | Arrays |
| Cambridge IGCSE Grade 9 Computer Science (0478) | 8. Programming |
| Cambridge IGCSE Grade 10 Computer Science (0478) | 8. Programming |
| Programming All levels Java | Arrays and Strings |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

