Repository navigation
Expand file tree
/
Copy pathArray.java
More file actions
174 lines (135 loc) · 3.86 KB
/
Copy pathArray.java
File metadata and controls
174 lines (135 loc) · 3.86 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
/**
* @author mcrwayfun
* @version v1.0
* @date Created in 2018/10/01
* @description
*/
public class Array<E> {
// 保存元素使用的数组
private E[] data;
// 数组中元素的个数
private int size;
@SuppressWarnings("unchecked")
public Array(int capacity) {
data = (E[]) new Object[capacity];
this.size = 0;
}
public Array() {
this(10);
}
// 获取容量
public int getCapacity() {
return data.length;
}
// 获取当前数组元素
public int getSize() {
return size;
}
// 判断数组是否为空
public boolean isEmpty() {
return size == 0;
}
// 向数组的末尾添加元素
void addLast(E e) {
add(size, e);
}
// 向数组的头部添加元素
void addFirst(E e) {
add(0, e);
}
// 在指定位置添加元素
void add(int index, E e) {
if (index < 0 || index > size)
throw new IllegalArgumentException("Add failed.index is invalid !");
if (size == data.length)
resize(2 * data.length);// 扩容两倍
for (int i = size - 1; i >= index; i--)
data[i + 1] = data[i];
data[index] = e;
size++;
}
// 获取指定位置的元素
public E get(int index) {
if (index < 0 || index >= size)
throw new IllegalArgumentException("Add failed.index is invalid !");
return data[index];
}
// 获取头部元素
public E getFirst() {
return get(0);
}
// 获取尾部元素
public E getLast() {
return get(size - 1);
}
// 替换指定位置的元素
public void set(int index, E e) {
if (index < 0 || index >= size)
throw new IllegalArgumentException("Set failed.index is invalid !");
data[index] = e;
}
// 判断数组中是否包含该元素
public boolean contains(E e) {
for (E x : data) {
if (x.equals(e)) {
return true;
}
}
return false;
}
// 查找元素e在数组中对应的索引,如果不存在则返回 -1
public int find(E e) {
for (int i = 0; i < size; i++)
if (data[i].equals(e))
return i;
return -1;
}
// 删除数组中指定索引的元素
public E remove(int index) {
if (index < 0 || index >= size)
throw new IllegalArgumentException("Remove failed.index is invalid !");
E ret = data[index];
for (int i = index + 1; i <= size - 1; i++)
data[i - 1] = data[i];
size--;
data[size] = null;// help gc
if (size == data.length / 4 && data.length / 2 != 0)
resize(data.length / 2);// 容量缩减为一半
return ret;
}
// 从数组中删除最后一个元素
public E removeLast() {
return remove(size - 1);
}
// 从数组中删除元素e
public E removeFirst() {
return remove(0);
}
// 删除数组中指定的元素e
public void removeElement(E e) {
int index = find(e);
if (index != -1)
remove(index);
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Array: size = %d , capacity = %d\n", size, data.length));
res.append('[');
for (int i = 0; i < size; i++) {
res.append(data[i]);
if (i != size - 1)
res.append(", ");
}
res.append(']');
return res.toString();
}
// 将数组容量改为newCapacity大小
@SuppressWarnings("unchecked")
private void resize(int newCapacity) {
E[] newData = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++)
newData[i] = data[i];
data = newData;
}
}