Skip to content

Commit bf7dceb

Browse files
Merge pull request #416 from ZHANGfeng-james/master
Update GenericArray.java
2 parents 8d49f28 + d38609d commit bf7dceb

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

java/05_array/GenericArray.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public int find(T e) {
6363

6464
// 在 index 位置,插入元素e, 时间复杂度 O(m+n)
6565
public void add(int index, T e) {
66-
checkIndex(index);
66+
checkIndexForAdd(index);
6767
// 如果当前元素个数等于数组容量,则将数组扩容为原来的2倍
6868
if (size == data.length) {
6969
resize(2 * data.length);
@@ -88,7 +88,7 @@ public void addLast(T e) {
8888

8989
// 删除 index 位置的元素,并返回
9090
public T remove(int index) {
91-
checkIndexForRemove(index);
91+
checkIndex(index);
9292

9393
T ret = data[index];
9494
for (int i = index + 1; i < size; i++) {
@@ -150,14 +150,14 @@ private void resize(int capacity) {
150150
}
151151

152152
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.");
155155
}
156156
}
157157

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.");
161161
}
162162
}
163-
}
163+
}

0 commit comments

Comments
 (0)