-1
我有一個鏈接列表,其中包含項目。該項目有一個searchkey,我需要訪問,所以我可以將其與我傳遞給tableDelete方法的字符串進行比較。如何訪問鏈接列表項中的對象
基本上,我們通過一個城市的名稱就是searchkey
喜歡說搜索鏈接列表,塞特是搜索鍵,我不知道如何head.getItem()
後訪問它能夠比較,所以我可以刪除它,或者我想要。
c = new City("Narragansett", "USA", 12000);
chart.tableInsert(c);
因此以上城市是鏈表
public boolean tableDelete(KT searchKey) {
if(head.getItem().equals(searchKey)){
head = head.getNext();
return true;
}
Node current = head.getNext();
Node prev = head;
while(current!= null){
if(current.getItem().equals(searchKey)){
prev.setNext(current.getNext());
return true;
}
prev = current;
current = current.getNext();
}
return false;
}
什麼問題? – Taylor
你能否詳細說明這個問題。它不是很清楚。 –