From d26c032a5a6380e99e1ee53f2bdaf98f731b8f12 Mon Sep 17 00:00:00 2001 From: processisanswer <41319181+processisanswer@users.noreply.github.com> Date: Sat, 12 Feb 2022 23:57:27 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=20SinglyLinkedList.java-=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=8F=AF=E9=87=8D=E5=A4=8D=E5=88=A0=E9=99=A4=E6=8C=87?= =?UTF-8?q?=E5=AE=9Avalue=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 举例:1->1->1->1 或者1->1->1->2->3->1类似数据组织形式,删除指定value=1时,可能获取不到合理结果。 --- java/06_linkedlist/SinglyLinkedList.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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; From cbc89284cc0bfabbef4adfcbad9ed518745fdb69 Mon Sep 17 00:00:00 2001 From: processisanswer <41319181+processisanswer@users.noreply.github.com> Date: Sun, 13 Feb 2022 12:03:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=9C=AA=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=85=83=E7=B4=A0=E7=9B=B4=E6=8E=A5=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=A4=B4=E9=83=A8=E6=88=96=E5=B0=BE=E9=83=A8=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GenericArray intArr = new GenericArray<>(); intArr.removeFirst();//或者intArr.removeLast(); 未添加元素,直接删除头部或尾部元素时,合理的异常应该为IndexOutOfBoundsException,不过原程序已经考虑的很周到了 --- java/05_array/GenericArray.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 +}