-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighArrayApp.java
More file actions
84 lines (72 loc) · 2.01 KB
/
HighArrayApp.java
File metadata and controls
84 lines (72 loc) · 2.01 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
//How do I run this?
class HighArray {
private long[] a;
private int nElems;
public HighArray(int max) {
a = new long[max];
nElems = 0;
}
public boolean find(long searchKey) {
int j;
for (j = 0; j < nElems; j++)
if (a[j] == searchKey)
break;
if (j == nElems)
return false;
else
return true;
}
public void insert(long value) { // FIX 1: Removed duplicate insert method
a[nElems] = value;
nElems++;
}
public boolean delete(long value) { // FIX 2: Changed "void boolean" → "boolean"
int j;
for (j = 0; j < nElems; j++) {
if (value == a[j])
break;
} // FIX 3: Moved not-found check outside the loop
if (j == nElems) {
return false;
} else {
for (int k = j; k < nElems - 1; k++) { // FIX 4: k++ instead of j++
a[k] = a[k + 1]; // FIX 5: nElems-1 to prevent index out of bounds
}
nElems--;
return true;
}
}
public void display() {
for (int j = 0; j < nElems; j++) {
System.out.print(a[j] + " ");
}
System.out.println("");
}
}
class HighArrayApp {
public static void main(String[] args) {
int maxSize = 100;
HighArray arr;
arr = new HighArray(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
int searchKey = 35;
if (arr.find(searchKey))
System.out.println("Found " + searchKey);
else
System.out.println("Can't find " + searchKey);
arr.delete(00);
arr.delete(55);
arr.delete(99);
arr.display();
}
}