我试图从链表中删除一个特定的字符串,但我一直得到空指针异常错误!任何帮助家伙?
public void delete(String deleteName) {
LinkedListNode current = head;
if(head==null){
System.out.println("Nothing to delete from the list! Insert data.");
}
// for head
else if(current.getName()==deleteName){
head=current.getNext();
}
// for tail
else if(findTail().getName() == deleteName){
while(current.getNext().getNext()!=null ){
current = current.getNext();
}
current.setNext(null);
}
else{
while(current.getNext().getName()!=deleteName){
current=current.getNext();
}
current.setNext(current.getNext().getNext());
}
System.out.println("");
System.out.println(deleteName +" has been deleted from the list.");
}
答案 0 :(得分:0)
做了一些编辑。现在工作!不要使用==来比较字符串。
public void delete(String deleteName) {
LinkedListNode current = head;
if(head==null){
System.out.println("Nothing to delete from the list! Insert data.");
}
// for head
else if(current.getName().equals(deleteName)){
head=current.getNext();
}
// for tail
else if(findTail().getName().equals(deleteName)){
while(current.getNext().getNext()!=null ){
current = current.getNext();
}
current.setNext(null);
}
else{
while(!current.getNext().getName().equals(deleteName)){
current=current.getNext();
}
current.setNext(current.getNext().getNext());
}
System.out.println("");
System.out.println(deleteName +" has been deleted from the list.");
} // end of delete()