-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArray.java
More file actions
121 lines (104 loc) · 2.65 KB
/
Copy pathMyArray.java
File metadata and controls
121 lines (104 loc) · 2.65 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
package Review;
public class MyArray {
private int count;
private int[] intArr;
public static final int ERROR_NUM = -9999999;
public int ARRAY_SIZE;
public MyArray() {
count = 0;
ARRAY_SIZE = 10;
intArr = new int[ARRAY_SIZE];
}
public MyArray(int size) {
System.out.println("super class");
count = 0;
ARRAY_SIZE = size;
intArr = new int[ARRAY_SIZE];
}
public int addElement(int data) {
if(count >= ARRAY_SIZE) {
System.out.println("not enough memory");
return ERROR_NUM;
}
intArr[count++] = data;
return data;
}
public int insertElement(int position, int data) {
if(count >= ARRAY_SIZE) {
System.out.println("not enough memory");
return ERROR_NUM;
}
if(position < 0 || position > count) {
System.out.println("position error");
return ERROR_NUM;
}
else {
for(int i = count-1; i >= position; i--) {
intArr[i+1] = intArr[i];
}
}
intArr[position] = data;
count++;
return data;
}
public int removeElement(int position) {
int ret = ERROR_NUM;
if(isEmpty()) {
System.out.println("There is not element");
return ret;
}
if(position < 0 || position >= count) {
System.out.println("position error");
return ret;
}
else {
ret = intArr[position];
for(int i = position; i < count-1; i++) {
intArr[i] = intArr[i+1];
}
}
count--;
return ret;
}
public int getElement(int position) {
if(position < 0 || position >= count) {
System.out.println("position error");
return ERROR_NUM;
}
return intArr[position];
}
public int getSize() {
return count;
}
public boolean isEmpty() {
if(count == 0) {
return true;
}
else {
return false;
}
}
public boolean isFull() {
if(count == ARRAY_SIZE) {
return true;
}
else {
return false;
}
}
public void printAll() {
if(isEmpty()) {
System.out.println("출력할 요소가 없습니다.");
return;
}
for(int i = 0; i < count; i++) {
System.out.println(intArr[i]);
}
}
public void removeAll() {
for(int i = 0; i < count; i++) {
intArr[i] = 0;
}
count = 0;
}
}