File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ public class Array {
3
+
4
+ public static void main (String [] args ) {
5
+ Scanner s = new Scanner (System .in ); // Input statement
6
+ System .out .println ("Enter the size of the array" );
7
+ int size = s .nextInt ();
8
+ int a [] = new int [size ];
9
+ int i ;
10
+
11
+ // To enter the initial elements
12
+ for (i =0 ;i <size ;i ++){
13
+ System .out .println ("Enter the element" );
14
+ a [i ] = s .nextInt ();
15
+ }
16
+
17
+ // To insert a new element(we are creating a new array)
18
+ System .out .println ("Enter the index at which the element should be inserted" );
19
+ int insert_pos = s .nextInt ();
20
+ System .out .println ("Enter the element to be inserted" );
21
+ int ins = s .nextInt ();
22
+ int size2 = size + 1 ;
23
+ int b [] =new int [size2 ];
24
+ for (i =0 ;i <size2 ;i ++){
25
+ if (i <= insert_pos ){
26
+ b [i ] = a [i ];
27
+ }
28
+ else {
29
+ b [i ] = a [i -1 ];
30
+ }
31
+ }
32
+ b [insert_pos ] = ins ;
33
+ for (i =0 ;i <size2 ;i ++){
34
+ System .out .println (b [i ]);
35
+ }
36
+
37
+ // To delete an element given the index
38
+ System .out .println ("Enter the index at which element is to be deleted" );
39
+ int del_pos = s .nextInt ();
40
+ for (i =del_pos ;i <size2 -1 ;i ++){
41
+ b [i ] = b [i +1 ];
42
+ }
43
+ for (i =0 ;i <size2 -1 ;i ++)
44
+ System .out .println (b [i ]);
45
+ }
46
+ }
You can’t perform that action at this time.
0 commit comments