-
Notifications
You must be signed in to change notification settings - Fork 0
/
arr_multi.java
74 lines (65 loc) · 2.39 KB
/
arr_multi.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.Arrays;
import java.util.Scanner;
class arr_multi{
public static void main(String[] a){
// Multidimensional Arrays
// 2-D Arrays
int[][] arr1 = new int[2][2];
arr1[0][0] = 12;
System.out.println("First Element of first dimension : " + arr1[0][0]);
Scanner scn = new Scanner(System.in);
// Properly Implementing a 4*4 2D array
int r1,c1,val = 1;
System.out.print("Enter the number of rows you want in your array : ");
r1 = scn.nextInt();
System.out.print("Enter the number of column you want in your array : ");
c1 = scn.nextInt();
int[][] arr2 = new int[r1][c1];
for(int i = 0;i < r1;i++){
for(int j = 0;j < c1;j++){
arr2[i][j] = val;
val++;
}
}
System.out.println("2D array after initiating as per your choice : " + Arrays.toString(arr2));
System.out.println("Similar Array in Tabular Format Below !");
for(int i = 0;i < r1;i++){
for(int j = 0;j < c1;j++){
System.out.print(arr2[i][j] + " ");
}
System.out.println();
}
System.out.println();
val = 1;
// 3D Arrays
int r2,c2,z2;
System.out.print("Enter the number of rows you want in your 3D array : ");
r2 = scn.nextInt();
System.out.print("Enter the number of columns you want in your 3D array : ");
c2 = scn.nextInt();
System.out.println("Enter the number of zindex you want in your 3D array : ");
z2 = scn.nextInt();
int[][][] array1 = new int[r2][c2][z2]; // Array Initialized
for(int i = 0;i < r2;i++){
for(int j = 0;j < c2;j++){
for(int k = 0;k < z2;k++){
array1[i][j][k] = val;
val++;
}
System.out.println();
}
System.out.println();
}
System.out.println("3D array in proper format below !");
for(int i = 0;i < r2;i++){
for(int j = 0;j < c2;j++){
for(int k = 0;k < z2;k++){
System.out.print(array1[i][j][k] + " ");
}
System.out.println();
}
System.out.println();
}
scn.close();
}
}