-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathCURD.java
62 lines (62 loc) · 1.54 KB
/
CURD.java
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
import java.util.*;
public class CURD
{
int ar[],m;
CURD(int a)
{
m=a;
ar=new int[m];
}
void CreateArray()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter elements of array");
for (int i=0;i<m;i++)
ar[i]=sc.nextInt();
}
void PrintArray()
{
for (int i=0;i<m;i++)
System.out.print(ar[i]+" ");
}
void UpdateElement(int newElement,int pos)
{
if(ar==null || pos>=m || pos<0){
System.out.println("Out of bound index");
return;
}
for (int i=0;i<m;i++)
{
if(pos==i+1)
ar[i]=newElement;
}
}
void DeleteElement(int pos)
{
if(ar==null || pos>=m || pos<0){
System.out.println("Out of bound index");
return;
}
for(int i=pos;i<m-1;i++)
ar[i]=ar[i+1];
m--;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter length of array");
int m=sc.nextInt();
CURD obj=new CURD(m);
obj.CreateArray();
obj.PrintArray();
System.out.println("Enter element to be updated also the position in which it is to be updated");
int Element=sc.nextInt();
int pos=sc.nextInt();
obj.UpdateElement(Element,pos);
obj.PrintArray();
System.out.println("Enter index to be deleted");
int index=sc.nextInt();
obj.DeleteElement(index);
obj.PrintArray();
}
}