-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultidimArray.java
54 lines (47 loc) · 1.54 KB
/
MultidimArray.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class MultidimArray{
public static void main(String args[]){
// we can have multidimensional arrays
// we can have an array of arrays
int arr[][] = {
{2,3,4},
{1,2,3},
{2,4,6}
};
// System.out.println(arr[0][0]);
// System.out.println(arr[0][1]);
// using nested loops
for( int i = 0; i<3; i++){
for (int j = 0; j<3; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
int arr1[][] = new int[4][3];
// for( int i = 0; i<3; i++){
// for (int j = 0; j<4; j++){
// System.out.print(arr1[i][j] + " ");
// }
// System.out.println();
// }
//
// int random = (int) (Math.random() * 10);
for( int i = 0; i<4; i++){
for (int j = 0; j<3; j++){
arr1[i][j] = (int) (Math.random() * 10);
}
}
// for( int i = 0; i<4; i++){
// for (int j = 0; j<3; j++){
// System.out.print(arr1[i][j] + " ");
// }
// System.out.println();
// }
// enhanced for loop
for (int n[] : arr1){ // here n is not a simple value, it is array also
// System.out.println(n);
for( int m : n){
System.out.println(m);// printing every value in array
}
}
}
}