-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathArray.java
272 lines (245 loc) · 4.44 KB
/
Array.java
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* 数组
*
* @author
* @version 2018/8/4
*/
public class Array<E> {
private E[] data;
private int size;
public Array(int capacity){
data = (E[])new Object[capacity];
size = 0;
}
public Array() {
this(10);
}
public int getSize(){
return size;
}
public int getCapacity(){
return data.length;
}
public boolean isEmpty(){
return size == 0;
}
/**
* 向数组头部插入一个元素
*
* @param e
* @return void
* @author ronglexie
* @version 2018/8/4
*/
public void addFirst(E e){
add(0,e);
}
/**
* 向数组尾部插入一个元素
*
* @param e
* @return void
* @author ronglexie
* @version 2018/8/4
*/
public void addLast(E e){
add(size,e);
}
/**
* 向数组指定位置插入一个元素
*
* @param index
* @param e
* @return void
* @author ronglexie
* @version 2018/8/4
*/
public void add(int index, E e){
if(index < 0 || index > size){
throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size");
}
if(size == data.length){
// throw new IllegalArgumentException("Add failed. Array is full.");
//数组动态扩容两倍
resize(2*data.length);
}
for (int i= size-1; i >= index; i--){
data[i+1] = data[i];
}
data[index] = e;
size++;
}
private void resize(int newCapacity) {
E[] newData = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
data = newData;
}
/**
* 获取指定位置的元素
*
* @param index
* @return E
* @author ronglexie
* @version 2018/8/4
*/
public E get(int index){
if(index < 0 || index >= size){
throw new IllegalArgumentException("Get failed. index is Illegal.");
}
return data[index];
}
/**
* 获取第一个元素
*
* @param
* @return E
* @author ronglexie
* @version 2018/8/4
*/
public E getFirst() {
return data[0];
}
/**
* 获取最后一个元素
*
* @param
* @return E
* @author ronglexie
* @version 2018/8/4
*/
public E getLast() {
return get(size - 1);
}
/**
* 修改指定位置的元素
*
* @param index
* @param e
* @return void
* @author ronglexie
* @version 2018/8/4
*/
public void set(int index, E e){
if (index < 0 || index >= size){
throw new IllegalArgumentException("Set failed. index is Illegal.");
}
data[index] = e;
}
/**
* 查看数组中是否包含某个元素
*
* @param e
* @return boolean
* @author ronglexie
* @version 2018/8/4
*/
public boolean contains(E e){
for (int i = 0; i < size; i++) {
if(data[i].equals(e)){
return true;
}
}
return false;
}
/**
* 查找元素在数组中的位置
*
* @param e
* @return int
* @author ronglexie
* @version 2018/8/4
*/
public int indexOf(E e){
for (int i = 0; i < size; i++) {
if(data[i].equals(e)){
return i;
}
}
return -1;
}
/**
* 移除数组中的一个元素
*
* @param index
* @return int
* @author ronglexie
* @version 2018/8/4
*/
public E remove(int index){
if (index < 0 || index >= size){
throw new IllegalArgumentException("Remove failed. index is Illegal.");
}
E result = data[index];
for (int i = index + 1; i < size; i++) {
data[i-1] = data[i];
}
size--;
//修改对象引用,垃圾回收机制回收
data[size] = null;
//动态缩小数组一半容量
if(size == data.length/4 && data.length/2 != 0){
resize(data.length/2);
}
return result;
}
/**
* 移除数组中的第一个元素
*
* @param
* @return E
* @author ronglexie
* @version 2018/8/4
*/
public E removeFirst(){
return remove(0);
}
/**
* 移除数组中的最后一个元素
*
* @param
* @return int
* @author ronglexie
* @version 2018/8/4
*/
public E removeLast(){
return remove(size - 1);
}
/**
* 移除数组中的某个元素
*
* @param e
* @return void
* @author ronglexie
* @version 2018/8/4
*/
public void removeElement(E e){
int index = indexOf(e);
if(index != -1){
remove(index);
}
}
/**
* 自定义toString方法
*
* @param
* @return java.lang.String
* @author ronglexie
* @version 2018/8/4
*/
@Override
public String toString(){
StringBuilder result = new StringBuilder();
result.append(String.format("Array: size = %d , capacity = %d\n",size,data.length));
result.append("[");
for (int i = 0; i < size; i++){
result.append(data[i]);
if(i != size - 1){
result.append(", ");
}
}
result.append("]");
return result.toString();
}
}