-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.java
More file actions
157 lines (143 loc) · 3.64 KB
/
ArrayList.java
File metadata and controls
157 lines (143 loc) · 3.64 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
package Array;
import Array._ArrayList;
public class ArrayList<T> implements _ArrayList<T> {
private Object[] data;
//数组长度
private int n;
//元素个数
private int count;
public ArrayList(int capacity) {
if (capacity > 0) {
this.data = new Object[capacity];
this.n = capacity;
} else if (capacity == 0){
this.data = new Object[0];
this.n = 0;
} else {
throw new IllegalArgumentException("illegal");
}
}
public ArrayList(T[] array){
if (array == null) {
throw new NullPointerException();
}
this.data = new Object[array.length];
for (int i = 0; i < array.length; i++) {
this.data[i] = array[i];
}
this.n = array.length;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public int length() {
return 0;
}
//下标获取元素
@Override
public T get(int index) {
if (index >= 0 && index < this.n) {
return (T)this.data[index];
}
return null;
}
//下标设置元素
@Override
public T set(int index, T content) {
if (index > 0 && index < this.n && content != null) {
T old = (T)this.data[index];
this.data[index] = content;
return old;
}
return null;
}
//下标加元素
@Override
public boolean add(int index, T content) {
if (data == null) {
return false;
}
if (index < 0) {
index = 0;
}
if (index > this.n) {
index = this.n;
}
//如果数组满
if (this.n == this.data.length) {
//temp copy
Object[] temp = this.data;
//new Container
this.data = new Object[temp.length*2];
//trans data
for (int i = 0; i < index; ++i) {
this.data[i] = temp[i];
}
}
//数据后移,index-last
for (int j = this.data.length-1; j >= index ; --j) {
this.data[j+1] = this.data[j];
}
//插入
this.data[index] = content;
this.n++;
return true;
}
//依据index删除元素
@Override
public T remove(int index) {
if (this.n !=0 && index >= 0 && index < this.n){
T oldElement = (T) this.data[index];
for (int i = index + 1; i < this.n ; i++) {
this.data[i] = this.data[i+1];
}
//设置尾巴
this.data[this.n - 1] = null;
this.n --;
return oldElement;
}
return null;
}
@Override
public boolean remove(T content) {
return false;
}
@Override
public boolean removeAll(T content) {
boolean result = false;
if (this.n !=0 && content != null){
int i = 0;
while (i < this.n){
if (content.equals(this.data[i])){
this.remove(i);
result = true;
}
i++;
}
}
return result;
}
@Override
public boolean add(T content) {
return false;
}
@Override
public void clear() {
}
@Override
public boolean contains(T content) {
return false;
}
@Override
public int indexOf(T content) {
if (content != null) {
for (int i = 0; i < this.n; i++) {
if (this.data[i].equals(content))
return i;
}
}
return -1;
}
}