@@ -63,7 +63,7 @@ public int find(T e) {
63
63
64
64
// 在 index 位置,插入元素e, 时间复杂度 O(m+n)
65
65
public void add (int index , T e ) {
66
- checkIndex (index );
66
+ checkIndexForAdd (index );
67
67
// 如果当前元素个数等于数组容量,则将数组扩容为原来的2倍
68
68
if (size == data .length ) {
69
69
resize (2 * data .length );
@@ -88,7 +88,7 @@ public void addLast(T e) {
88
88
89
89
// 删除 index 位置的元素,并返回
90
90
public T remove (int index ) {
91
- checkIndexForRemove (index );
91
+ checkIndex (index );
92
92
93
93
T ret = data [index ];
94
94
for (int i = index + 1 ; i < size ; i ++) {
@@ -150,14 +150,14 @@ private void resize(int capacity) {
150
150
}
151
151
152
152
private void checkIndex (int index ) {
153
- if (index < 0 || index > size ) {
154
- throw new IllegalArgumentException ("Add failed! Require index >=0 and index <= size." );
153
+ if (index < 0 || index >= size ) {
154
+ throw new IllegalArgumentException ("Add failed! Require index >=0 and index < size." );
155
155
}
156
156
}
157
157
158
- private void checkIndexForRemove (int index ) {
159
- if (index < 0 || index >= size ) {
160
- throw new IllegalArgumentException ("remove failed! Require index >=0 and index < size." );
158
+ private void checkIndexForAdd (int index ) {
159
+ if (index < 0 || index > size ) {
160
+ throw new IllegalArgumentException ("remove failed! Require index >=0 and index <= size." );
161
161
}
162
162
}
163
- }
163
+ }
0 commit comments