-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyObjectArray.java
More file actions
113 lines (90 loc) · 2.64 KB
/
Copy pathMyObjectArray.java
File metadata and controls
113 lines (90 loc) · 2.64 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
package array;
public class MyObjectArray {
private int count;
private Object[] objectArr;
public int ARRAY_SIZE;
public static final int ERROR_NUM = -99999999;
public MyObjectArray() {
count = 0;
ARRAY_SIZE = 10;
objectArr = new Object[ARRAY_SIZE];
}
public MyObjectArray(int size) {
count = 0;
ARRAY_SIZE = size;
objectArr = new Object[ARRAY_SIZE];
}
public void addElement(Object object) {
if(count >= ARRAY_SIZE) { //꽉 찬 경우
System.out.println("not enough memory");
return;
}
objectArr[count++] = object;
}
public void insertElement(int position, Object object) {
if(count >= ARRAY_SIZE) { //꽉 찬 경우
System.out.println("not enough memory");
return;
}
if(position < 0 || position > count-1) { //position index error
System.out.println("insert position index error");
return;
}
for(int i = count-1; i >= position; i--) { //마지막 요소부터 하나씩 뒤로 이동
objectArr[i+1] = objectArr[i];
}
objectArr[position] = object;
count++;
}
public Object removeElement(int position) {
Object ret = null;
if(isEmpty()) {
System.out.println("There is no element");
return ret;
}
if(position < 0 || position >= count) { //position index error
System.out.println("remove position index error");
return ret;
}
ret = objectArr[position];
for(int i = position; i < count; i++) {
objectArr[i] = objectArr[i+1];
}
count--;
return ret;
}
public int getSize() {
return count;
}
public boolean isEmpty() {
if(count == 0) {
return true;
}
else {
return false;
}
}
public Object getElement(int position) {
Object ret = null;
if(position < 0 || position >= count) {
System.out.println("검색 위치 오류. 현재 리스트의 개수는 " + count + "개 입니다.");
return ret;
}
return objectArr[position];
}
public void printAll() {
if(count == 0) {
System.out.println("출력할 요소가 없습니다.");
return;
}
for(int i = 0; i < count; i++) {
System.out.println(objectArr[i]);
}
}
public void removeAll() {
for(int i = 0; i < count; i++) {
objectArr[i] = null;
}
count = 0;
}
}