-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArray2D.java
More file actions
40 lines (37 loc) · 1.19 KB
/
Array2D.java
File metadata and controls
40 lines (37 loc) · 1.19 KB
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
32
33
34
35
36
37
38
39
40
public class Array2D {
public static void main(String[] args) {
int a[] = { 1, 2, 3, 4 };
int b[] = { 5, 6, 7, 8 };
int c[] = { 9, 10, 11, 12 };
System.out.println(a[2]); // 3
// Multidimensional Array
int d[][] = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 0, 1, 2 }
};
System.out.println(d[2][0]); // 9
for (int i = 0; i < d.length; i++) { // counting rows
for (int j = 0; j < d[i].length; j++) { // counting columns
System.out.print(" " + d[i][j]);
}
System.out.println(); // 1 2 3 4
// 5 6 7 8
// 9 0 1 2
}
// Jagged Array
int e[][] = {
{ 1, 2, 3, 4 },
{ 5, 6, 7 },
{ 9, 0, 1, 2, 8, 3 }
};
for (int i[] : e) { // counting rows
for (int l : i) { // counting columns
System.out.print(" " + l);
}
System.out.println(); // 1 2 3 4
// 5 6 7
// 9 0 1 2 8 3
}
}
}