-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertArrayElement.java
More file actions
38 lines (34 loc) · 1.18 KB
/
Copy pathInsertArrayElement.java
File metadata and controls
38 lines (34 loc) · 1.18 KB
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
import java.util.Scanner;
public class InsertArrayElement {
//METHOD TO INPUT ELEMENT AT A PARTICULAR INDEX
static int[] insertElement(int arr[]){
Scanner sc= new Scanner(System.in);
for(int i=0; i<arr.length; i++){
System.out.print("Enter index to input element: ");
int index= sc.nextInt();
if(index<arr.length){
System.out.print("Enter the value of index " + index + ": ");
arr[index]=sc.nextInt();
}
else{
System.out.println("PLEASE INPUT INDEX LESS THAN " + arr.length + "!");
}
}
return arr;
}
//METHOD TO PRINT ARRAY
static void displayArray(int a[]){
for(int i=0; i<a.length; i++){
System.out.print(a[i] + " ");
}
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
//INPUT ARRAY SIZE AND ELEMENTS
System.out.print("Enter array size: ");
int size= sc.nextInt();
int arr[]= new int[size];
int a[]= insertElement(arr); //STORING THE ARRAY THAT IS RETURNING AFTER INSERTING ELEMENT
displayArray(a);
}
}