forked from PrajaktaSathe/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGssArray.java
More file actions
129 lines (116 loc) · 3.36 KB
/
GssArray.java
File metadata and controls
129 lines (116 loc) · 3.36 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
119
120
121
122
123
124
125
126
127
128
129
/*
Name - Jayneel Shah
Date - 23/10/21
Topic - Application of classes in Java
Program to implement growable self sorting array(GSSArray)
*/
import java.util.Scanner;
class GSSArray {
int i = 0;
private int[] arr;
private int size;
private int lastIndex = -1;
//default constructor
GSSArray(int n) {
arr = new int[n];
size = n;
}
//insert function
public void insert(int value) {
//if size is less than defined size regular addition
if (size > i) {
lastIndex++;
arr[lastIndex] = value;
i++;
display();
}
//if the size is more then it first increases the size and then performs addition in the array
else {
increaseSize();
lastIndex++;
arr[lastIndex] = value;
i++;
display();
}
}
//incrase size function - it doubles the size and copie the elements of old array in the new array
private void increaseSize() {
int[] arr1 = new int[arr.length * 2];
for (int i = 0; i < arr.length; i++) {
arr1[i] = arr[i];
}
arr = arr1;
}
//to check whether the element to be delted is present in the array is present or not
public boolean delete(int value) {
int pos = -1;
for (int index = 0; index < lastIndex; index++) {
if (arr[index] == value) {
pos = index;
break;
}
}
if (pos == -1) {
return false;
} else {
System.out.println(pos);
for (int i = pos; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
i--;
lastIndex = lastIndex - 1;
return true;
}
}
//display function
void display() {
sort();
System.out.print("Elements in array:- \n");
for (int i = 0; i <= lastIndex; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
//function to sort the incoming elements
void sort() {
int temp;
for (int i = 0; i <= lastIndex; i++) {
for (int j = i + 1; j <= lastIndex; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
}
class Gssarray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
GSSArray gs = new GSSArray(5);
int choice, val;
boolean ans;
while (true) {
System.out.println("Enter 1 to insert");
System.out.println("Enter 2 to delete");
System.out.println("Enter 3 to exit");
choice = sc.nextInt();
if (choice == 1) {
System.out.print("Enter the value to enter in array: ");
val = sc.nextInt();
gs.insert(val);
} else if (choice == 2) {
System.out.print("Enter the value to delete: ");
val = sc.nextInt();
ans = gs.delete(val);
System.out.println(ans);
gs.display();
} else if (choice == 3) {
System.out.println("Thank You!");
break;
}
}
sc.close();
}
}