-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericArray.java
More file actions
238 lines (213 loc) · 4.82 KB
/
GenericArray.java
File metadata and controls
238 lines (213 loc) · 4.82 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package ds.array;
import org.omg.CORBA.Object;
import java.util.Objects;
/**
* @author xiangdotzhaoAtwoqutechcommacom
* @date 2019/11/16
*/
public class GenericArray<T> {
/**
* 泛型数组
*/
private T[] data;
/**
* 数组大小
*/
private int len;
/**
* 构造函数
*
* @param capacity 数组容量
*/
public GenericArray(int capacity) {
data = (T[]) new Object[capacity];
len = 0;
}
/**
* 默认构造函数
*/
public GenericArray() {
this(10);
}
public static void main(String[] args) {
GenericArray<Person> arr = new GenericArray<>();
System.out.println(arr);
arr.addFirst(new Person(12, "Millie"));
// ArrayStoreException
System.out.println(arr);
}
/**
* 获取数组大小
*
* @return 数组大小
*/
public int len() {
return this.len;
}
/**
* 判断数组是否为空
*
* @return true if 数组为空
*/
public boolean isEmpty() {
return len == 0;
}
/**
* 设置 index 位置的元素
*
* @param index 索引
* @param e 将要设置的值
*/
public void set(int index, T e) {
checkIndexForRemove(index);
data[index] = e;
}
/**
* 获取 index 位置上的元素
*
* @param index 索引
* @return 元素
*/
public T get(int index) {
checkIndexForRemove(index);
return data[index];
}
/**
* 数组是否包含 e
*
* @param e 元素 e
* @return true if 数组包含指定元素
*/
public boolean contains(T e) {
return find(e) != -1;
}
/**
* 获取对应元素的下标
*
* @param e 元素 e
* @return 元素所在的位置
*/
public int find(T e) {
for (int i = 0; i < len; i++) {
if (Objects.equals(e, data[i])) {
return i;
}
}
return -1;
}
/**
* 数组插入操作
*
* @param index 插入的位置
* @param e 插入的元素
*/
public void add(int index, T e) {
checkIndex(index);
if (len == data.length) {
resize(2 * data.length);
}
for (int i = len - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = e;
len++;
}
/**
* 数组向头部插入元素
*
* @param e 插入的元素
*/
public void addFirst(T e) {
add(0, e);
}
/**
* 数组向尾部插入元素
*
* @param e 待插入的元素
*/
public void addLast(T e) {
add(len, e);
}
/**
* 删除元素
*
* @param index 待删除元素的位置
* @return 删除的元素
*/
public T remove(int index) {
checkIndexForRemove(index);
T ret = data[index];
for (int i = index + 1; i < len; i++) {
data[i - 1] = data[i];
}
len--;
data[len] = null;
if (len == data.length / 4 && data.length / 2 != 0) {
resize(data.length / 2);
}
return ret;
}
/**
* 删除首位元素
*
* @return 首位元素
*/
public T removeFirst() {
return remove(0);
}
/**
* 删除末尾元素
*
* @return 末尾元素
*/
public T removeLast() {
return remove(len - 1);
}
/**
* 删除指定元素
*
* @param e 指定元素
*/
public void removeElement(T e) {
int index = find(e);
if (-1 != index) {
remove(index);
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(String.format("Array len = %d, capacity = %d \n", len, data.length));
builder.append("[");
for (int i = 0; i < len; i++) {
builder.append(data[i]);
if (i != len - 1) {
builder.append(",");
}
}
builder.append("]");
return builder.toString();
}
/**
* 扩容或缩容操作
*
* @param capacity 扩容或缩容后的大小
*/
private void resize(int capacity) {
T[] newData = (T[]) new Object[capacity];
for (int i = 0; i < len; i++) {
newData[i] = data[i];
}
data = newData;
}
private void checkIndex(int index) {
if (index < 0 || index > len) {
throw new IllegalArgumentException("Add failed! Require index >= 0 and index <= len");
}
}
private void checkIndexForRemove(int index) {
if (index < 0 || index >= len) {
throw new IllegalArgumentException("Remove failed! Require index >= 0 and index < len");
}
}
}