From 5bda2369466ff0cd81950946b681901b7e7966a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=B7=BB?= <2han9wen71an@gmail.com> Date: Wed, 1 Nov 2023 10:59:41 +0800 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8while?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E4=BB=A3=E6=9B=BFfor=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/linked_list/LinkedList.java | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/data-structures/src/main/java/linked_list/LinkedList.java b/data-structures/src/main/java/linked_list/LinkedList.java index f41077d..4269868 100644 --- a/data-structures/src/main/java/linked_list/LinkedList.java +++ b/data-structures/src/main/java/linked_list/LinkedList.java @@ -1,5 +1,7 @@ package linked_list; +import java.util.Objects; + /** * @author 小傅哥,微信:fustack * @description 实现双向链表 @@ -60,22 +62,22 @@ public boolean addLast(E e) { @Override public boolean remove(Object o) { - if (o == null) { - for (Node x = first; x != null; x = x.next) { - if (x.item == null) { - unlink(x); - return true; - } - } - } else { - for (Node x = first; x != null; x = x.next) { - if (o.equals(x.item)) { - unlink(x); - return true; - } + // 在循环外部声明变量,用于记录是否找到匹配项 + boolean found = false; + + // 遍历链表 + Node x = first; + while (x != null) { + if (Objects.equals(o, x.item)) { + // 找到匹配项,调用 unlink 方法移除节点 + unlink(x); + found = true; + break; // 找到匹配项后立即跳出循环 } + x = x.next; } - return false; + + return found; } E unlink(Node x) { From e6facb5ccbcd3092d4bb5068c093ca96ef8dd6ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=B7=BB?= <2han9wen71an@gmail.com> Date: Wed, 1 Nov 2023 11:27:52 +0800 Subject: [PATCH 2/3] =?UTF-8?q?docs:=20=E6=B7=BB=E5=8A=A0=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=EF=BC=8C=E6=96=B9=E4=BE=BF=E7=90=86=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data-structures/src/main/java/linked_list/LinkedList.java | 1 + 1 file changed, 1 insertion(+) diff --git a/data-structures/src/main/java/linked_list/LinkedList.java b/data-structures/src/main/java/linked_list/LinkedList.java index 4269868..3371b05 100644 --- a/data-structures/src/main/java/linked_list/LinkedList.java +++ b/data-structures/src/main/java/linked_list/LinkedList.java @@ -127,6 +127,7 @@ public void printLinkList() { Node node(int index) { if (index < (size >> 1)) { + // 如果索引小于链表长度的一半,则从头部开始遍历链表 Node x = first; for (int i = 0; i < index; i++) x = x.next; From 8de4585552d11933d47cc0ddf7f84b5037fff539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=B7=BB?= <2han9wen71an@gmail.com> Date: Wed, 1 Nov 2023 13:27:51 +0800 Subject: [PATCH 3/3] =?UTF-8?q?docs:=20=E6=B7=BB=E5=8A=A0=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=EF=BC=8C=E6=96=B9=E4=BE=BF=E7=90=86=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data-structures/src/main/java/array_list/ArrayList.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/data-structures/src/main/java/array_list/ArrayList.java b/data-structures/src/main/java/array_list/ArrayList.java index fb1008e..3c8dc95 100644 --- a/data-structures/src/main/java/array_list/ArrayList.java +++ b/data-structures/src/main/java/array_list/ArrayList.java @@ -45,10 +45,13 @@ public boolean add(E e) { // 判断扩容操作 if (minCapacity - elementData.length > 0) { int oldCapacity = elementData.length; + // 扩容为原容量的 1.5 倍 int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) { + // 如果计算得到的新容量小于 minCapacity,则直接使用 minCapacity newCapacity = minCapacity; } + // 扩容数组 elementData = Arrays.copyOf(elementData, newCapacity); } // 添加元素