-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighArray.java
More file actions
119 lines (104 loc) · 2.16 KB
/
HighArray.java
File metadata and controls
119 lines (104 loc) · 2.16 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
package dsa;
public class HighArray {
private long[] a;
private int nElems;
private long maxValue;
// constructor
public HighArray(int max){
a = new long[max];
nElems = 0;
}
public boolean find(long searchKey){
// find specified value
int j;
for(j=0; j<nElems; j++)
// for each element,
if(a[j] == searchKey)
// found item?
break;
// exit loop before end
if(j == nElems)
// gone to end?
return false;
// yes, can’t find it
else
return true;
// no, found it
} // end find()
// put element into array
public void insert(long value){
a[nElems] = value;
// insert it
nElems++;
// increment size
}
public boolean delete(long value){
int j;
for(j=0; j<nElems; j++)
// look for it
if( value == a[j] )
break;
if(j==nElems)
// can’t find it
return false;
else{
// found it
for(int k=j; k<nElems; k++) // move higher ones down
a[k] = a[k+1];
nElems--;
// decrement size
return true;
}
} // end delete()
public void displayAnItem(long value){
for(int j=0; j<nElems; j++){
if( value == a[j] ){
System.out.print(a[j] + " "); // display it
System.out.println("");
}else {
System.out.println("Value " + value + " was not found!");
break;
}
}
}
public long getMax(){
for (int i = 1; i < a.length; i++ ){
if (a[i-1] < a[i]){
maxValue = a[i];
return maxValue;
}
}
return -1;
} // end getMax()
public boolean removeMax(long value){
if( value == maxValue ){
for(int j=0; j < a.length; j++){
if (value == a[j]){
a[j] = a[j+1];
nElems--;
}
}
System.out.println("Max Value Removed: " + value);
return true;
}else{
return false;
}
}
public static void main(String[] args){
int maxSize = 100;
int searchKey = 35;
HighArray arr = new HighArray(maxSize); // create the array
arr.insert(21);
arr.insert(19);
arr.insert(17);
arr.insert(15);
arr.insert(39);
arr.insert(21);
long result = arr.getMax();
if (result != -1){
System.out.println("Max Value " + result);
}
arr.removeMax(result);
arr.displayAnItem(result);
}
} // end class HighArray