We could implement a matrix in Java as a two-dimensional (2D) array:
int[][] matrix = new int[n][m];
This matrix has
n rows and m columns. To populate it with elements, we'll use a nested for loop:
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
matrix[i][j] = i + j; // just an example
Example: 14. If we set
n = m = 3 and use the above code, we will get the matrix: $\begin{bmatrix} 0 & 1 & 2 \\ 1 & 2 & 3 \\ 2 & 3 & 4 \end{bmatrix}$.