-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.java
More file actions
193 lines (178 loc) · 4.1 KB
/
Array.java
File metadata and controls
193 lines (178 loc) · 4.1 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
import java.util.Arrays;
/**
* @author tailor
* @create 2020/3/22 - 10:03
* @mail wql2014302721@gmail.com
*/
public class Array<T> {
private T[] data;
private int size;
/**
* 构造方法
* @param capacity
*/
public Array(int capacity){
data = (T[])new Object[capacity];
size = 0;
}
/**
* 无参构造方法
*/
public Array(){
this(10);
}
/**
* 获取元素个数
* @return
*/
public int getSize() {
return size;
}
/**
* 获取数组容量
*/
public int getCapacity(){
return data.length;
}
/**
* 判断数组是否为空
* @return
*/
public boolean isEmpty(){
return size == 0;
}
/**
* 在数组最后面添加元素
* @param e
*/
public void addLast(T e){
add(size, e);
}
/**
* 在数组最前面添加元素
* @param e
*/
public void addFirst(T e){
add(0, e);
}
/**
* 在 index 位置处添加元素 e
* @param index
* @param e
*/
public void add(int index, T e){
if(size == data.length){
// throw new IllegalArgumentException("Add failed. Array is full.");
resize(size*2);
}
if(index < 0 || index > size){
throw new IllegalArgumentException("Add failed. Require index >=0 and index <= size.");
}
for(int i=size-1; i>=index; i--){
data[i+1] = data[i];
}
data[index] = e;
size++;
}
/**
* 获取数组index位置处的元素
* @param index
* @return
*/
public T get(int index){
if(index < 0 || index>=size){
throw new IllegalArgumentException("Get failed. Index is illegal.");
}
return data[index];
}
/**
* 修改 index 位置处的元素
* @param index
* @param e
*/
public void set(int index, T e){
if(index < 0 || index>=size){
throw new IllegalArgumentException("Set failed. Index is illegal.");
}
data[index] = e;
}
/**
* 判断数组中是否包含某一个元素
* @param e
* @return
*/
public boolean contains(T e){
for(int i=0; i<size; i++){
if(e.equals(data[i])) {
return true;
}
}
return false;
}
/**
* 查找数组中某一个元素对应的索引
* @param e
* @return
*/
public int findIndex(T e){
for(int i=0; i<size; i++){
if(e.equals(data[i])){
return i;
}
}
return -1;
}
/**
* 删除数组中index位置的元素, 返回删除的元素
* @param index
*/
public T remove(int index){
if(index < 0 || index>=size){
throw new IllegalArgumentException("Remove failed. Index is illegal");
}
T res = data[index];
for(int i=index; i<size-1; i++){
data[i] = data[i+1];
}
size--;
data[size] = null;
if(size == data.length/4 && 0 != data.length/2){
resize(data.length/2);
}
return res;
}
public T removeFirst(){
return remove(0);
}
public T removeLast(){
return remove(size-1);
}
public void removeElement(T e){
int index = findIndex(e);
if(-1 != index){
remove(index);
}
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(String.format("Array: Size=%d, capacity=%d\n", this.size, data.length));
sb.append("[");
for (int i = 0; i < size; i++) {
if(i==size-1){
sb.append(data[i]);
}else{
sb.append(data[i]+",");
}
}
sb.append("]");
return sb.toString();
}
private void resize(int newCapacity) {
T[] newData =(T[]) new Object[newCapacity];
for(int i=0; i<size; i++){
newData[i] = data[i];
}
data = newData;
}
}