-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicArray.java
More file actions
187 lines (152 loc) · 5.45 KB
/
DynamicArray.java
File metadata and controls
187 lines (152 loc) · 5.45 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
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
177
178
179
180
181
182
183
184
185
186
187
//Developed by: Barzy Yasin Karim.
import java.util.Scanner;
//import java.util.Random;
class DynamicArray {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Random rn = new Random();
System.out
.print("Implementing arraylist from array. \nBy: Barzy Yasin Karim.\n\n<--Welcome to dynamic array program--> \n\n");
System.out.print("* Please enter the array size: ");
int size = sc.nextInt();
int arr[] = new int[size];
int j = 11; // we can use user input or random class for the array
// values. but i prefer that it will be better in static bcs
// it's just for test.
// the array values staticly starts from (11) till enfinity.
for (int i = 0; i < arr.length; i++) {
// System.out.print("enter arr["+i+"]= ");
// arr[i]=rn.nextInt(9)+1;
arr[i] = j++;
}
System.out.println("\nYour array is:");
for (int i = 0; i < arr.length; i++) {
System.out.println("arr[" + i + "]= " + arr[i]);
}
while (true) {
System.out.print("\n------------menu------------\n");
System.out.println("tap ( 1 ) to add a new value.");
System.out.println("tap ( 2 ) to see the array.");
System.out.println("tap ( 3 ) to update a value.");
System.out.println("tap ( 4 ) to delete a value.");
System.out.println("tap ( 5 ) to swap two values.");
System.out.println("tap ( 6 ) to exit the program.");
System.out.print("\n- Enter your input here -> ");
int crud = sc.nextInt();
switch (crud) {
case 1: // adding index and value
int arrTempAdd[] = new int[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
arrTempAdd[i] = arr[i];
}
int newValueIndex = arrTempAdd.length - 1;
System.out.print("# Enter new value in arr[" + newValueIndex
+ "]: ");
arrTempAdd[newValueIndex] = sc.nextInt();
arr = new int[arrTempAdd.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = arrTempAdd[i];
}
for (int i = 0; i < arr.length; i++) {
System.out.println("arr[" + i + "]= " + arr[i]);
}
// System.out.println("\n");
break;
case 2: // printing all the array index. but the commented area is
// for printing an index value.
// System.out.print("# Enter the index number to see its value: ");
// int arrGetValue = sc.nextInt();
// if (arrGetValue > -1 && arrGetValue < arr.length)
// System.out.println("The value of arr[" + arrGetValue
// + "]= " + arr[arrGetValue]);
// else
// System.out.println("arr[" + arrGetValue
// + "] doesn't exist! ");
// show the array content.
for (int i = 0; i < arr.length; i++) {
System.out.println("arr[" + i + "]= " + arr[i]);
}
break;
case 3: // updating index value
System.out
.print("# Enter the index number \n of the value you want to change: ");
int arrSetValue = sc.nextInt();
if (arrSetValue < 0 || arrSetValue > arr.length - 1) {
System.out.println("arr[" + arrSetValue
+ "] doesn't exist! ");
break;
}
System.out.print("arr[" + arrSetValue + "]= "
+ arr[arrSetValue] + ".");
System.out.print("\n- Enter new value of arr[" + arrSetValue
+ "]: ");
arr[arrSetValue] = sc.nextInt();
// show the array content.
for (int i = 0; i < arr.length; i++) {
System.out.println("arr[" + i + "]= " + arr[i]);
}
break;
case 4: // deleting an index
System.out
.print("# Enter the index number to delete his value: ");
int arrDelIndex = sc.nextInt();
if (arrDelIndex < 0 || arrDelIndex > arr.length - 1) {
System.out.println("arr[" + arrDelIndex
+ "] doesn't exist! ");
break;
}
// 19009 is prime number
arr[arrDelIndex] = 0;
for (int i = arrDelIndex; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
int arrTempDel[] = new int[arr.length - 1];
for (int i = 0; i < arrTempDel.length; i++) {
arrTempDel[i] = arr[i];
}
arr = new int[arrTempDel.length];
for (int i = 0; i < arrTempDel.length; i++) {
arr[i] = arrTempDel[i];
}
// show the array content.
for (int i = 0; i < arr.length; i++) {
System.out.println("arr[" + i + "]= " + arr[i]);
}
break;
case 5: // swapping two values.
System.out
.print("# Enter the index number\n of the First value to swap: ");
int arrSwapIndex1 = sc.nextInt();
if (arrSwapIndex1 < 0 || arrSwapIndex1 > arr.length - 1) {
System.out.println("arr[" + arrSwapIndex1
+ "] doesn't exist! ");
break;
}
System.out
.print("# Enter the index number\n of the Second value to swap: ");
int arrSwapIndex2 = sc.nextInt();
if (arrSwapIndex2 < 0 || arrSwapIndex2 > arr.length - 1) {
System.out.println("arr[" + arrSwapIndex2
+ "] doesn't exist! ");
break;
}
int swapTemp = arr[arrSwapIndex1];
arr[arrSwapIndex1] = arr[arrSwapIndex2];
arr[arrSwapIndex2] = swapTemp;
System.out.println();
// show the array content.
for (int i = 0; i < arr.length; i++) {
System.out.println("arr[" + i + "]= " + arr[i]);
}
break;
case 6: // exit the program
System.out
.print("*Hope you get benefits <3 \n Have a good day...");
return;
default:
System.out
.println(" Sorry, your input number is out of the range!!!\n Please look down the menu again.");
}
}
}
}