-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDArray.java
More file actions
161 lines (132 loc) · 4.49 KB
/
DArray.java
File metadata and controls
161 lines (132 loc) · 4.49 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package me.smartstore.arrays;
import me.smartstore.exception.ElementNotFoundException;
import me.smartstore.exception.EmptyArrayException;
import me.smartstore.exception.NullArgumentException;
public class DArray<T> implements Collections<T> { // Dynamic Array
protected static final int DEFAULT = 10;
protected T[] arrays;
protected int size; //배열 원소의 개수
protected int capacity; //배열의 전체 크기
public DArray() throws ClassCastException {
arrays = (T[]) new Object[DEFAULT];
capacity = DEFAULT;
}
public DArray(int initial) throws ClassCastException {
arrays = (T[]) new Object[initial];
capacity = initial;
}
public DArray(T[] arrays) {
this.arrays = arrays;
capacity = arrays.length;
size = arrays.length;
}
/////////////////////////////////////////
// add, set, get, pop, indexOf, size, capacity (for dynamic-sized array)
@Override
public int size() {
return size;
}
// 배열에 얼마나 capacity 남아있는지 외부에 알려줄 필요가 없기 때문에 <protected>으로 정의
protected int capacity() {
return capacity;
}
//arrays의 index 위치에 있는 값을 반환
@Override
public T get(int index) throws IndexOutOfBoundsException {
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
return arrays[index];
}
/**
* @param: ...
* @return: ...
* @throws: IndexOutOfBoundsException
* @throws: NullArgumentException
* */
@Override
public void set(int index, T object) throws IndexOutOfBoundsException, NullArgumentException {
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
if (object == null) throw new NullArgumentException();
arrays[index] = object;
}
//object의 index를 반환
@Override
public int indexOf(T object) throws NullArgumentException, ElementNotFoundException {
if (object == null) throw new NullArgumentException(); // not found (instead of throwing exception)
for (int i = 0; i < size; i++) {
if (arrays[i] == null) continue;
if (arrays[i].equals(object)) return i;
}
throw new ElementNotFoundException(); // not found
}
// 배열의 cap이 부족한 경우
@Override
public void add(T object) throws NullArgumentException {
if (object == null) throw new NullArgumentException(); // if argument is null, do not add null value in array
if (size < capacity) {
arrays[size] = object;
size++;
} else {
grow();
add(object);
}
}
//원하는 index 위치로 데이터 넣기
@Override
public void add(int index, T object) throws IndexOutOfBoundsException, NullArgumentException {
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
if (object == null) throw new NullArgumentException();
if (size < capacity) {
for (int i = size-1; i >= index ; i--) {
arrays[i+1] = arrays[i];
}
arrays[index] = object;
size++;
} else {
grow();
add(index, object);
}
}
//배열의 맨 끝 원소 pop
@Override
public T pop() {
// if (size == 0) return null;
//
// T popElement = arrays[size-1];
// arrays[size-1] = null;
// size--;
// return popElement;
return pop(size-1);
}
//index의 원소 pop
@Override
public T pop(int index) throws IndexOutOfBoundsException {
if (size == 0) throw new EmptyArrayException();
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
T popElement = arrays[index];
arrays[index] = null; // 삭제됨을 명시적으로 표현
for (int i = index+1; i < size; i++) {
arrays[i-1] = arrays[i];
}
arrays[size-1] = null;
size--;
return popElement;
}
//원하는 데이터 pop
@Override
public T pop(T object) {
return pop(indexOf(object));
}
protected void grow() {
capacity *= 2; // doubling
arrays = java.util.Arrays.copyOf(arrays, capacity);
// size는 그대로
}
@Override
public String toString() {
String toStr = "";
for (int i = 0; i < size; i++) {
toStr += (arrays[i] + "\n");
}
return toStr;
}
}