FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/07-Java-Programs/075-Three-Dimensional-Array.java at main · shaikbasha-dev/Java · GitHub
shaikbasha-dev
/
Java
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
07-Java-Programs
/
075-Three-Dimensional-Array.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
135 lines (98 loc) · 3.89 KB
Breadcrumbs
Java
/
07-Java-Programs
/
075-Three-Dimensional-Array.java
Copy path
File metadata and controls
135 lines (98 loc) · 3.89 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* ============================================================================
* Program Name : Three-Dimensional Array
* File Name : 075-Three-Dimensional-Array.java
* Class Name : ThreeDimensionalArray
*
* Description:
* This program accepts the dimensions and elements of a three-dimensional
* array from the user and displays all the elements layer by layer.
*
* Objective:
* - Understand user input using the Scanner class.
* - Learn how to declare and initialize a three-dimensional array.
* - Learn how to access and display 3D array elements using nested loops.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import
java
.
util
.
Scanner
;
public
class
ThreeDimensionalArray
{
// The main() method is the entry point of every Java application.
public
static
void
main
(
String
[]
args
) {
// Create a Scanner object to read input from the keyboard.
Scanner
scanner
=
new
Scanner
(
System
.
in
);
// Ask the user to enter the number of layers.
System
.
out
.
print
(
"Enter the Number of Layers: "
);
// Read the number of layers.
int
layers
=
scanner
.
nextInt
();
// Ask the user to enter the number of rows.
System
.
out
.
print
(
"Enter the Number of Rows: "
);
// Read the number of rows.
int
rows
=
scanner
.
nextInt
();
// Ask the user to enter the number of columns.
System
.
out
.
print
(
"Enter the Number of Columns: "
);
// Read the number of columns.
int
columns
=
scanner
.
nextInt
();
// Check whether the entered dimensions are valid.
if
(
layers
<=
0
||
rows
<=
0
||
columns
<=
0
) {
// Display an error message.
System
.
out
.
println
(
"Please enter positive values for layers, rows, and columns."
);
}
else
{
// Declare a three-dimensional integer array.
int
[][][]
array
=
new
int
[
layers
][
rows
][
columns
];
// Ask the user to enter the array elements.
System
.
out
.
println
(
"Enter the Array Elements:"
);
// Read all the elements using nested loops.
for
(
int
i
=
0
;
i
<
layers
;
i
++) {
System
.
out
.
println
(
"Layer "
+ (
i
+
1
) +
":"
);
for
(
int
j
=
0
;
j
<
rows
;
j
++) {
for
(
int
k
=
0
;
k
<
columns
;
k
++) {
// Store the current element.
array
[
i
][
j
][
k
] =
scanner
.
nextInt
();
}
}
}
// Display the array elements.
System
.
out
.
println
(
"Three-Dimensional Array:"
);
// Traverse the array using nested loops.
for
(
int
i
=
0
;
i
<
layers
;
i
++) {
System
.
out
.
println
(
"Layer "
+ (
i
+
1
) +
":"
);
for
(
int
j
=
0
;
j
<
rows
;
j
++) {
for
(
int
k
=
0
;
k
<
columns
;
k
++) {
// Display the current element.
System
.
out
.
print
(
array
[
i
][
j
][
k
] +
" "
);
}
// Move to the next row.
System
.
out
.
println
();
}
// Leave a blank line after each layer.
System
.
out
.
println
();
}
// Example Output:
// Enter the Number of Layers: 2
// Enter the Number of Rows: 2
// Enter the Number of Columns: 2
// Enter the Array Elements:
// Layer 1:
// 1 2
// 3 4
// Layer 2:
// 5 6
// 7 8
//
// Three-Dimensional Array:
// Layer 1:
// 1 2
// 3 4
//
// Layer 2:
// 5 6
// 7 8
}
// Close the Scanner object to release system resources.
scanner
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL