-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArray.java
More file actions
124 lines (98 loc) · 2.9 KB
/
Copy pathMyArray.java
File metadata and controls
124 lines (98 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
119
120
121
122
123
124
package array;
//Array : 선형 자료구조(인접한 데이터간의 관계가 1:1인 자료구조) 중 하나로,
//-동일한 데이터 타입을 순서에 따라 관리하는 자료구조
//-정해진 크기가 있음
//-자료의 논리적 위치와 물리적 위치가 동일
//-> 요소의 추가 및 제거 시 다른 요소들의 이동이 필요
public class MyArray {
int[] intArr; //int array
int count; //개수
public int ARRAY_SIZE;
public static final int ERROR_NUM = -99999999;
public MyArray() {
count = 0;
ARRAY_SIZE = 10;
intArr = new int[ARRAY_SIZE];
}
public MyArray(int size) {
count = 0;
ARRAY_SIZE = size;
intArr = new int[ARRAY_SIZE];
}
public void addElement(int num) {
if(count >= ARRAY_SIZE) {
System.out.println("not enough memory");
return;
}
intArr[count++] = num;
}
public void insertElement(int position, int num) {
int i;
if(count >= ARRAY_SIZE) { //꽉 찬 경우
System.out.println("not enough memory");
return;
}
if(position < 0 || position > count) { //position index error
System.out.println("insert Error");
return;
}
for(i = count-1; i >= position; i--) { //하나씩 뒤로 이동
intArr[i+1] = intArr[i];
}
intArr[position] = num;
count++;
}
public int removeElement(int position) {
int i;
int ret = ERROR_NUM;
if(isEmpty()) { //비어있을 경우
System.out.println("There is no element");
return ret;
}
if(position < 0 || position >= count) { //position index error
System.out.println("remove error");
return ret;
}
ret = intArr[position];
for(i = position; i < count-1; i++) {
intArr[i] = intArr[i+1];
}
count--;
return ret;
}
public int getSize() {
return count;
}
public boolean isEmpty() {
if(count == 0) {
return true;
}
else {
return false;
}
}
public int getElement(int position) {
if(position < 0 || position > count-1) { //position index error
System.out.println("검색 위치 오류. 현재 리스트의 개수는 " + count + "개 입니다.");
return ERROR_NUM;
}
return intArr[position];
}
public void printAll() {
int i;
if(count == 0) {
System.out.println("출력할 내용이 없습니다.");
return;
}
for(i = 0; i<count; i++) {
System.out.println(intArr[i]);
}
}
public void removeAll() {
int i;
for(i = 0; i < count; i++) {
intArr[i] = 0;
}
count = 0;
}
}