Skip to content

Commit

Permalink
Merge pull request wangzheng0822#416 from ZHANGfeng-james/master
Browse files Browse the repository at this point in the history
Update GenericArray.java
  • Loading branch information
wangzheng0822 authored Nov 17, 2019
2 parents 8d49f28 + d38609d commit bf7dceb
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions java/05_array/GenericArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public int find(T e) {

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

// 删除 index 位置的元素,并返回
public T remove(int index) {
checkIndexForRemove(index);
checkIndex(index);

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

private void checkIndex(int index) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("Add failed! Require index >=0 and index <= size.");
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Add failed! Require index >=0 and index < size.");
}
}

private void checkIndexForRemove(int index) {
if(index < 0 || index >= size) {
throw new IllegalArgumentException("remove failed! Require index >=0 and index < size.");
private void checkIndexForAdd(int index) {
if(index < 0 || index > size) {
throw new IllegalArgumentException("remove failed! Require index >=0 and index <= size.");
}
}
}
}

0 comments on commit bf7dceb

Please sign in to comment.