Skip to content

Commit c85e15f

Browse files
committed
feat: Iterate over 2D array with enhanced for-loops and print elements
🧠 Logic: - Defines a 3×3 matrix using a 2D array literal. - Uses an outer for-each loop (`for (int[] row : matrix)`) to iterate through each row. - Uses an inner for-each loop (`for (int element : row)`) to iterate through and print each element. - Correctly prints `element` instead of `matrix.length` to display the actual array values. Signed-off-by: Somesh diwan <[email protected]>
1 parent eff0c28 commit c85e15f

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

Section9ArrayAB/Array IMP/TwoDArray2.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
public class TwoDArray2 {
22
public static void main(String[] args) {
3-
int B[][] = {{1, 2, 3}, {2, 4, 6}, {1, 3, 5}};
4-
5-
for(int x[]:B)
6-
{
7-
for(int y:x)
8-
{
9-
/*System.out.println(y);*/
10-
System.out.println(B.length);
3+
int[][] matrix = {{1, 2, 3}, {2, 4, 6}, {1, 3, 5}};
4+
for (int[] row : matrix) {
5+
for (int element : row) {
6+
System.out.print(element+" "); // Print the actual element instead of matrix.length
117
}
128
}
139
}

0 commit comments

Comments
 (0)