java - Java双向链表删除方法
问题描述
我的问题是我的删除方法没有删除我要删除的节点并给我一个无限循环。
public void delete(String name){
Node current = head;
boolean checker = false;
while(current != null && current.name != name && checker != true){
try{
if(current.name.equals(name)){
Node p = current.previous;
Node q = current.next;
/*The code can somehow get through only above this line, below here its not anymore*/
p.next = q;
q.previous = p;
System.out.println("Item successfully deleted.");
checker = true;
}else if(!current.name.equals(name) && current == tail){
System.out.println("Item not found.");
}
current = current.next;
} catch(NullPointerException e){}
}
}
我在这里寻求有关我的问题的提示或提示(抱歉我的英语不好)
解决方案
您正在检查您是否已到达列表的末尾,current == tail
但没有突破它。您可以break
在else if
.
除此之外,您还==
用于比较字符串。我不确定你为什么在那里添加它并且可以将其删除。此外,您必须(几乎总是)永远不要捕获 NullPointerException。
推荐阅读
- python - 如何在 Django 中组织缓存?
- javascript - 如何停止/配置 Neovis Physics
- api - Flutter:DioError [DioErrorType.DEFAULT]:RangeError(索引):无效值:只有有效值为0:
- firebase - Firebase 身份验证与 Flutter 中的存储相结合
- swift - Xcode:“来自调试器的消息:无法附加”
- laravel - Laravel 页面重定向显示 404 错误
- go - 你如何连接到 AMQP 1.0 主题而不是在 Golang 中排队
- swift - 为 Swiftui 视图转换 @State 的计算属性
- python - 如何使用 Python 中的自适应阈值从 numpy ndarray 类型的 CT 图像中分割骨骼
- regex - 正则表达式 - 匹配一个句子,但如果存在,则忽略某些单词