Skip to content

Commit 6306e80

Browse files
committed
Update ArrayList.java
1 parent 7d53f32 commit 6306e80

File tree

1 file changed

+4
-6
lines changed
  • group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure

1 file changed

+4
-6
lines changed

group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public void add(Object obj) {
5151
* @param obj
5252
*/
5353
public void add(int index, Object obj) {
54-
rangeCheck(index);
54+
if (index > size || index < 0)
55+
throw new IndexOutOfBoundsException("索引越界异常");
56+
5557
ensureCapacity();
5658

5759
System.arraycopy(elementData, index, elementData, index + 1, size-index);
@@ -151,11 +153,7 @@ private void ensureCapacity() {
151153
*/
152154
private void rangeCheck(int index) {
153155
if(index < 0 || index >= size) {
154-
try {
155-
throw new Exception("索引异常");
156-
} catch (Exception e) {
157-
e.printStackTrace();
158-
}
156+
throw new IndexOutOfBoundsException("索引越界异常");
159157
}
160158
}
161159

0 commit comments

Comments
 (0)