FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java-Algorithms/insert_delete_in_array.java at master · atifs/Java-Algorithms · GitHub
atifs
/
Java-Algorithms
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Java-Algorithms
/
insert_delete_in_array.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
46 lines (42 loc) · 1.14 KB
Breadcrumbs
Java-Algorithms
/
insert_delete_in_array.java
Copy path
File metadata and controls
46 lines (42 loc) · 1.14 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
import
java
.
util
.*;
public
class
Array
{
public
static
void
main
(
String
[]
args
) {
Scanner
s
=
new
Scanner
(
System
.
in
);
// Input statement
System
.
out
.
println
(
"Enter the size of the array"
);
int
size
=
s
.
nextInt
();
int
a
[] =
new
int
[
size
];
int
i
;
// To enter the initial elements
for
(
i
=
0
;
i
<
size
;
i
++){
System
.
out
.
println
(
"Enter the element"
);
a
[
i
] =
s
.
nextInt
();
}
// To insert a new element(we are creating a new array)
System
.
out
.
println
(
"Enter the index at which the element should be inserted"
);
int
insert_pos
=
s
.
nextInt
();
System
.
out
.
println
(
"Enter the element to be inserted"
);
int
ins
=
s
.
nextInt
();
int
size2
=
size
+
1
;
int
b
[] =
new
int
[
size2
];
for
(
i
=
0
;
i
<
size2
;
i
++){
if
(
i
<=
insert_pos
){
b
[
i
] =
a
[
i
];
}
else
{
b
[
i
] =
a
[
i
-
1
];
}
}
b
[
insert_pos
] =
ins
;
for
(
i
=
0
;
i
<
size2
;
i
++){
System
.
out
.
println
(
b
[
i
]);
}
// To delete an element given the index
System
.
out
.
println
(
"Enter the index at which element is to be deleted"
);
int
del_pos
=
s
.
nextInt
();
for
(
i
=
del_pos
;
i
<
size2
-
1
;
i
++){
b
[
i
] =
b
[
i
+
1
];
}
for
(
i
=
0
;
i
<
size2
-
1
;
i
++)
System
.
out
.
println
(
b
[
i
]);
}
}
Back
|
FazBrowse Home
|
New Git URL