-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayOps.java
More file actions
118 lines (114 loc) · 2.9 KB
/
arrayOps.java
File metadata and controls
118 lines (114 loc) · 2.9 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
package OCJP;
import java.util.*;
class myArray{
private long[] value;
private int numElements;
// Creating the constructor
public myArray(int size){
value = new long[size];
numElements = 0;
}
// Create a search class
public boolean search(long key){
int j;
for(j=0;j<numElements;j++)
if(value[j] == key)
break;
if(j == numElements)
return false;
else
return true;
}
// Creating the insert method
public void insert(long val){
value[numElements] = val;
numElements++;
}
// Create a delete method
public boolean delete(long val){
int j;
for(j=0;j<numElements;j++)
if(val == value[j])
break;
if(j == numElements)
return false;
else
{
int k;
for(k=j;k<numElements;k++){
value[k] = value[k+1];
}
numElements--;
return true;
}
}
// Display method for the array
public void display(){
for(int i = 0;i<numElements;i++){
System.out.print(value[i]+" ");
}
System.out.println();
}
// insertion of getMax
public long getMax(){
long maxVal = value[0];
for(int i = 0;i<numElements;i++){
if(value[i]>maxVal)
maxVal = value[i];
}
return maxVal;
}
// remove the maximum Element from the array
public long removeMax(long maxVal){
maxVal = value[0];
for(int i = 0;i<numElements;i++){
if(value[i]>maxVal)
maxVal = value[i];
}
int j;
for(j=0;j<numElements;j++){
if(value[j]==maxVal)
break;
}
if(j==numElements)
return 0;
else
{
int k;
for(k=j;k<numElements;k++){
value[k] = value[k+1];
numElements--;
}
}
return maxVal;
}
}
public class arrayOps {
public static void main(String args[]){
myArray arr = new myArray(10);
arr.insert(10);
arr.insert(20);
arr.insert(25);
arr.insert(35);
arr.insert(47);
arr.insert(89);
arr.insert(71);
arr.insert(99);
arr.display();
arr.delete(20);
arr.display();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a key to search:");
long key = sc.nextLong();
if(arr.search(key)){
System.out.println("Found key");
}
else{
System.out.println("Key not Found");
}
long maxVal = arr.getMax();
System.out.println("The max value in the array is:"+maxVal);
long removeMaxVal = arr.removeMax(maxVal);
arr.display();
}
}