Skip to content

Commit 8218980

Browse files
committed
feat: add NestedLoop2DArrayTraversal to demonstrate 2D array population and iteration
Implemented NestedLoop2DArrayTraversal.java to showcase filling a 3x5 matrix with sequential values using nested loops and printing each element with its row and column indices. Highlights basic 2D array traversal and indexing logic. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent e78d22b commit 8218980

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 NestedLoop2DArrayTraversal {
2+
public static void main(String[] args) {
3+
int[][] dataTable = new int[3][5]; // Declare a 3x5 matrix
4+
5+
// Populate the array with sequential numbers starting from 10
6+
int num = 10;
7+
for (int i = 0; i < dataTable.length; i++) {
8+
for (int j = 0; j < dataTable[i].length; j++) {
9+
dataTable[i][j] = num++;
10+
}
11+
}
12+
13+
// Iterate and print each element with its position
14+
for (int i = 0; i < dataTable.length; i++) {
15+
for (int j = 0; j < dataTable[i].length; j++) {
16+
System.out.println("Element at [" + i + "][" + j + "] = " + dataTable[i][j]);
17+
}
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)