Explanation
This Java program demonstrates how to declare, initialize, and traverse a two-dimensional (2D) array. A 2D array can be thought of as a table with rows and columns.
- Initialization: The code creates a 2D array named
numand populates it with static values:{{2, 5}, {12, 10}}. This creates a $2 \times 2$ matrix where the first row contains 2 and 5, and the second row contains 12 and 10. - Outer Loop: The first
forloop (using variablei) iterates through the rows of the array.num.lengthreturns the number of rows (which is 2). - Inner Loop: Inside the outer loop, a second
forloop (using variablej) iterates through the individual elements of the current row.num[i].lengthreturns the number of columns in that specific row. - Printing: The program uses
System.out.printto display the values. It adds brackets[and]to visually separate the rows, creating a formatted string representation of the matrix on the console. Note that because of the logic used, a trailing comma appears after every number.