-
Notifications
You must be signed in to change notification settings - Fork 1
/
Curd.java
176 lines (155 loc) · 5.14 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.util.Scanner;
public class arrayExamples {
enum ArrayOperations{
INSERT,DELETE,SEARCH,UPDATE,PRINT,EXIT // index start from 0
}
// CURD operations - insert/delete/Search/update/Print
int currentSize;
int arr[];
arrayExamples(int capacity){
this.arr = new int[capacity];
this.currentSize = 0;
}
void insert(int index, int value){ // at which index what you want to insert
if(index>currentSize){
throw new RuntimeException("Index cannot be greater than current size");
}
if(currentSize==arr.length){
System.out.println("arrray is full cant add elements");
return;
}
// now shifting
for(int i = currentSize-1; i>=index; i--){
arr[i+1]=arr[i];
}
arr[index]=value;
currentSize++;
print();
System.out.println();
}
void delete(int index){
if(currentSize==0){
System.out.println("Array is Empty..,");
return;
}
// Shifting
for(int i = index; i<currentSize-1; i++){
arr[i]=arr[i+1];
}
arr[currentSize-1]=0; // to put 0 at the last index
currentSize--;
print();
System.out.println();
}
// leniar search
int search (int value){
int index = -1;
for(int i =0 ; i<arr.length; i++){
if(arr[i]==value){
index = i;
}
}
return index;
}
void update(int valueToSearch, int valueToUpdate){
int index = search(valueToSearch);
if(index==-1){
return;
}
else{
arr[index] = valueToUpdate;
print();
System.out.println("Value updated");
}
}
void print(){
for(int i : arr){
System.out.print(i+",");
}
}
public static void main(String[] args) {
// 4 types of array declarations -
// int declararion1[] = {14,2,4};
// int[]declararion2 = {12,2,4};
// int declararion3[] = new int[]{1,2,3,4};
// int declararion4[] = new int[4];
// for(int i: declararion1){
// System.out.print(i);
// }
// CURD operations - insert/delete/Search/update/Print
// arrayExamples obj = new arrayExamples(5);
// obj.insert(0, 10);
// obj.insert(1, 20);
// obj.insert(2, 30);
// obj.insert(3, 40);
// obj.insert(4, 50);
// obj.delete(1);
// obj.insert(2, 33);
// obj.update(40, 11 );
// Output -----
// 10,0,0,0,0,
// 10,20,0,0,0,
// 10,20,30,0,0,
// 10,20,30,40,0,
// 10,20,30,40,50,
// 10,30,40,50,0,
// 10,30,33,40,50,
// 10,30,33,11,50,Value updated
arrayExamples obj = new arrayExamples(10);
Scanner sc = new Scanner(System.in);
int choice;
int index;
int value;
int value2;
while(true){
System.out.println("Array CRUD Operation: ");
System.out.println("1. Insert Operations");
System.out.println("2. Remove Operations");
System.out.println("3. Search Operations");
System.out.println("4. Update Operations");
System.out.println("5. Print Operations");
System.out.println("6. Exit");
choice = sc.nextInt();
ArrayOperations enumChoice = ArrayOperations.values()[choice-1];
switch (enumChoice) {
case INSERT:
System.out.println("Enter the Index: ");
index = sc.nextInt();
System.out.println("Enter the value: ");
value = sc.nextInt();
obj.insert(index,value);
break;
case DELETE:
System.out.println("Enter the index: ");
index = sc.nextInt();
obj.delete(index);
break;
case SEARCH:
System.out.println("Enter the value to search: ");
value = sc.nextInt();
if(obj.search(value)==-1){
System.out.println("Not found");
}
else{
System.out.println("The index is :"+obj.search(value));
}
break;
case UPDATE:
System.out.println("Enter the value to repalce: ");
value = sc.nextInt();
System.out.println("Enter the value to update: ");
value2 = sc.nextInt();
obj.update(value, value2);
break;
case PRINT:
obj.print();
break;
case EXIT:
return;
default:
System.out.println("Wrong choice");
break;
}
}
}
}