-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayCRUD.java
More file actions
59 lines (49 loc) · 1.28 KB
/
ArrayCRUD.java
File metadata and controls
59 lines (49 loc) · 1.28 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Arrays;
public class ArrayCRUD {
int arr[];
int currentSize;
ArrayCRUD(int n) {
this.arr = new int[n];
this.currentSize = 0;
}
void insert(int index, int item) {
if(index > currentSize) {
System.out.println("Index cannot be greater than current size...");
return;
}
// Shift elements
for(int i = currentSize-1; i >= index; i--) {
arr[i+1] = arr[i];
}
arr[index] = item;
currentSize++;
}
void delete(int index) {
if(currentSize == 0) {
System.out.println("Array is empty...Cannot delete elements");
return;
}
for(int i = index; i < currentSize - 1; i++) {
arr[i] = arr[i+1];
}
arr[currentSize-1] = 0;
currentSize--;
}
void search() {
}
void update() {
}
void print() {
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
}
public static void main(String[] args) {
ArrayCRUD obj = new ArrayCRUD(5);
obj.insert(0, 3);
obj.insert(1, 6);
obj.insert(2, 9);
obj.insert(1, 7);
obj.print();
}
}