-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArray.java
More file actions
57 lines (53 loc) · 1.55 KB
/
MyArray.java
File metadata and controls
57 lines (53 loc) · 1.55 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
package Array;
import java.util.Arrays;
public class MyArray {
private int[] arr;
private int numEle;
public MyArray(int size){
this.arr = new int[size];
this.numEle=0;
}
public int search(int ele){
for (int i = 0; i < numEle; i++) {
if (arr[i] == ele) {
return i;
}
}
return -1;
}
public void insert(int ele){
if (arr.length == numEle) {
System.out.println("Error pro array is full !!");
}else {
arr[numEle++]=ele;
System.out.println("successful process...");
}
}
public void delete(int ele){
if (numEle==0) {
System.out.println("Error pro array is empty !!");
}else {
int check = search(ele);
if (check == -1) {
System.out.println("Error pro this element is not find !!!");
} else if (check == (numEle - 1)) {
arr[check]=0;
} else if (check == (numEle - 2) && (numEle == arr.length)) {
for (int i = check; i < numEle - 1; i++) {
arr[i] = arr[i + 1];
}
arr[numEle-1] =0;
numEle--;
} else {
for (int i = check; i < numEle - 1; i++) {
arr[i] = arr[i + 1];
}
numEle--;
}
System.out.println("successful process...");
}
}
public void display(){
System.out.println(Arrays.toString(arr));
}
}