Skip to content

Commit e78d22b

Browse files
committed
feat: add Iterating2DArraysWithLoop to demonstrate nested loop traversal
Implemented a simple Java example showcasing iteration over a 2D integer array using nested for-loops. Prints each row and element sequentially, illustrating matrix traversal and structured console output formatting. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 13cc926 commit e78d22b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Iterating2DArraysWithLoop {
2+
public static void main(String[] args) {
3+
// Define a 3x3 matrix with predefined values
4+
int[][] matrix = {
5+
{10, 20, 30}, // Initialize row 1 of the matrix
6+
{40, 50, 60}, // Initialize row 2 of the matrix
7+
{70, 80, 90} // Initialize row 3 of the matrix
8+
};
9+
10+
// Print a header to indicate the start of matrix iteration
11+
System.out.println("Iterating over a 3x3 matrix:");
12+
for (int i = 0; i < matrix.length; i++) { // Loop over each row of the matrix
13+
System.out.print("Row " + (i + 1) + ": "); // Print the row number
14+
for (int j = 0; j < matrix[i].length; j++) { // Loop over each column in the current row
15+
System.out.print(matrix[i][j] + " "); // Print each element in the row followed by a space
16+
}
17+
System.out.println(); // Move to the next line after printing all columns in a row
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)