-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathArrayOperationsDemo.java
More file actions
118 lines (105 loc) · 3.54 KB
/
ArrayOperationsDemo.java
File metadata and controls
118 lines (105 loc) · 3.54 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
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
import java.util.Scanner;
public class ArrayOperationsDemo {
int arr[];
int size;
ArrayOperationsDemo(int capacity) {
arr = new int[capacity];
size = 0; // current elements in array
}
void insert(int index, int element) {
if (index > size) {
throw new RuntimeException("Index can't be more than the current size");
}
if (size == arr.length) {
throw new RuntimeException("Array is Full....");
}
for (int i = size - 1; i >= index; i--) {
arr[i + 1] = arr[i];
}
arr[index] = element;
size++;
}
void remove(int index) {
if (size == 0) {
throw new RuntimeException("Array is Empty");
}
for (int i = index; i < size; i++) {
arr[i] = arr[i + 1];
}
size--;
}
void print() {
System.out.println("*********** PRINT ************");
for (int ele : arr) {
System.out.println(ele);
}
}
void update(int element, int newElement) {
int index = search(element);
if (index == -1) {
System.out.println("Element Not found so can't update....");
return;
}
arr[index] = newElement;
}
int search(int element) {
for (int i = 0; i < size; i++) {
if (arr[i] == element) {
return i;
}
}
return -1; // Not Found
}
public static void main(String[] args) {
ArrayOperationsDemo opr = new ArrayOperationsDemo(5);
Scanner scanner = new Scanner(System.in);
int index;
int element;
int newElement;
while (true) {
System.out.println("1. Add New Element");
System.out.println("2, Remove Element");
System.out.println("3. Print Element");
System.out.println("4. Search Element");
System.out.println("5. Update Element");
System.out.println("6. Exit");
System.out.println("Enter the Choice");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the Index ");
index = scanner.nextInt();
System.out.println("Enter the Element");
element = scanner.nextInt();
opr.insert(index, element);
break;
case 2:
System.out.println("Enter the Index ");
index = scanner.nextInt();
opr.remove(index);
break;
case 3:
opr.print();
break;
case 4:
System.out.println("Enter the Element");
element = scanner.nextInt();
String msg = opr.search(element) == -1 ? "Element Not Found" : "Element found " + element;
System.out.println(msg);
break;
case 5:
System.out.println("Enter the Element");
element = scanner.nextInt();
System.out.println("Enter the New Element");
newElement = scanner.nextInt();
opr.update(element, newElement);
break;
case 6:
scanner.close();
return;
default:
System.out.println("WrongChoice.....");
}
} // Loopends
}
}