-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest46a.java
31 lines (22 loc) · 1.74 KB
/
Test46a.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Main
{
public static void main(String[] args)
{
int a[][] = {{ 1, 2, 3 },{ 4, 5, 6, 7 },{ 8, 9 } };
for (int i=0; i<a.length; i++)
{
for (int j=0; j<a[i].length; j++)
System.out.print(a[i][j] + " ");
System.out.println();
}
}
}
/*
1 int a[][] = {{ 1, 2, 3 },{ 4, 5, 6, 7 },{ 8, 9 } };: This line declares and initializes a two-dimensional array named a. The array a is initialized with three rows and different column lengths: the first row has 3 elements, the second row has 4 elements, and the third row has 2 elements.
2 for (int i=0; i<a.length; i++): This line starts a loop that iterates over the rows of the array a. The loop variable i represents the current row index.
3 for (int j=0; j<a[i].length; j++): This line starts another loop nested inside the outer loop. It iterates over the columns of the current row a[i]. The loop variable j represents the current column index.
4 System.out.print(a[i][j] + " ");: This line prints the current element of the array a at position [i][j] using System.out.print(). The element is followed by a space to separate it from the next element.
5 System.out.println();: This line prints a newline character to move to the next line after printing all the elements of the current row.
After going through all the iterations of the inner loop, the program moves to the next row by incrementing i in the outer loop. This process continues until all rows and columns of the array have been traversed.
Finally, when the loops finish executing, the program exits, and the output is displayed. The output shows each element of the array printed in a matrix-like format, where each row is on a separate line and elements within a row are separated by spaces.
*/