diff --git a/java/05_array/GenericArray.java b/java/05_array/GenericArray.java index 631a1bae..6f1a4fee 100644 --- a/java/05_array/GenericArray.java +++ b/java/05_array/GenericArray.java @@ -156,8 +156,11 @@ private void checkIndex(int index) { } private void checkIndexForRemove(int index) { + if (size ==0){ + throw new IndexOutOfBoundsException("remove failed! Require size>0"); + } if(index < 0 || index >= size) { throw new IllegalArgumentException("remove failed! Require index >=0 and index < size."); } } -} \ No newline at end of file +} diff --git a/java/06_linkedlist/SinglyLinkedList.java b/java/06_linkedlist/SinglyLinkedList.java index cf3d1545..d3d63235 100644 --- a/java/06_linkedlist/SinglyLinkedList.java +++ b/java/06_linkedlist/SinglyLinkedList.java @@ -49,6 +49,7 @@ public void insertToHead(Node newNode) { //顺序插入 //链表尾部插入 + public void insertTail(int value){ Node newNode = new Node(value, null); @@ -143,12 +144,12 @@ public void deleteByValue(int value) { // 可重复删除指定value的代码 /* - if (head != null && head.data == value) { + while (head != null && head.data == value) { head = head.next; } Node pNode = head; - while (pNode != null) { + while (pNode != null && pNode.next!=null) { if (pNode.next.data == data) { pNode.next = pNode.next.next; continue;