FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/07-Java-Programs/076-Jagged-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
/
076-Jagged-Array.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
121 lines (89 loc) · 3.6 KB
Breadcrumbs
Java
/
07-Java-Programs
/
076-Jagged-Array.java
Copy path
File metadata and controls
121 lines (89 loc) · 3.6 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
/*
* ============================================================================
* Program Name : Jagged Array
* File Name : 076-Jagged-Array.java
* Class Name : JaggedArray
*
* Description:
* This program demonstrates the use of a Jagged Array (Array of Arrays)
* in Java. A jagged array allows each row to have a different number
* of columns.
*
* Objective:
* - Understand the concept of Jagged Arrays.
* - Learn how to create rows with different column sizes.
* - Learn how to read and display Jagged Array elements.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import
java
.
util
.
Scanner
;
public
class
JaggedArray
{
// 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 rows.
System
.
out
.
print
(
"Enter the Number of Rows: "
);
// Read the number of rows.
int
rows
=
scanner
.
nextInt
();
// Check whether the entered number of rows is valid.
if
(
rows
<=
0
) {
// Display an error message.
System
.
out
.
println
(
"Please enter a positive number of rows."
);
}
else
{
// Declare a jagged array.
int
[][]
jaggedArray
=
new
int
[
rows
][];
// Create each row with different column sizes.
for
(
int
i
=
0
;
i
<
rows
;
i
++) {
// Ask the user to enter the number of columns for the current row.
System
.
out
.
print
(
"Enter the Number of Columns for Row "
+ (
i
+
1
) +
": "
);
// Read the number of columns.
int
columns
=
scanner
.
nextInt
();
// Allocate memory for the current row.
jaggedArray
[
i
] =
new
int
[
columns
];
}
// Ask the user to enter the array elements.
System
.
out
.
println
(
"Enter the Jagged Array Elements:"
);
// Read all elements of the jagged array.
for
(
int
i
=
0
;
i
<
jaggedArray
.
length
;
i
++) {
System
.
out
.
println
(
"Row "
+ (
i
+
1
) +
":"
);
for
(
int
j
=
0
;
j
<
jaggedArray
[
i
].
length
;
j
++) {
// Store the current element.
jaggedArray
[
i
][
j
] =
scanner
.
nextInt
();
}
}
// Display the jagged array.
System
.
out
.
println
(
"Jagged Array Elements:"
);
// Traverse the jagged array.
for
(
int
i
=
0
;
i
<
jaggedArray
.
length
;
i
++) {
for
(
int
j
=
0
;
j
<
jaggedArray
[
i
].
length
;
j
++) {
// Display the current element.
System
.
out
.
print
(
jaggedArray
[
i
][
j
] +
" "
);
}
// Move to the next row.
System
.
out
.
println
();
}
// Example Output:
// Enter the Number of Rows: 3
// Enter the Number of Columns for Row 1: 2
// Enter the Number of Columns for Row 2: 4
// Enter the Number of Columns for Row 3: 3
// Enter the Jagged Array Elements:
// Row 1:
// 10 20
// Row 2:
// 30 40 50 60
// Row 3:
// 70 80 90
// Jagged Array Elements:
// 10 20
// 30 40 50 60
// 70 80 90
}
// Close the Scanner object to release system resources.
scanner
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL