-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertKeyElement.java
More file actions
40 lines (34 loc) · 1015 Bytes
/
InsertKeyElement.java
File metadata and controls
40 lines (34 loc) · 1015 Bytes
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
package arrays;
import java.util.Scanner;
class InsertElement{
Scanner sc = new Scanner(System.in);
void display(int[] a,int key,int index) {
for(int i = a.length;i>index;i++) {
a[i] = a[i-1];
a[index] = key;
}
System.out.println("Array Element After Inserting Key");
for(int i = 0;i<a.length;i++)
System.out.println(a[i]);
}
public void readArray() {
System.out.println("Enter the array size :");
int n = sc.nextInt();
int[] a = new int[n];
System.out.println("Enter the array elements");
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
System.out.println("Enter the key elemenent to be inserted:");
int key = sc.nextInt();
System.out.println("Enter the index of key elemenent to be inserted:");
int index = sc.nextInt();
display(a,key,index);
}
}
public class InsertKeyElement {
public static void main(String[] args) {
// TODO Auto-generated method stub
InsertElement ie = new InsertElement();
ie.readArray();
}
}