我正在使用Java S2SE製作一個簡單的原型項目。目標是製作一個文本文件,逐行閱讀,填充鏈接列表,然後要求用戶輸入名字和第二個名字。在Java中搜索LinkedList
文本文件的格式是:
firstnamesecondname手機之家移動電話2辦公
contact1contact2手機家園mobile2s辦公室
我在文本文件連接第一名稱及第二名稱。
然後我要求用戶輸入第一個和第二個名稱,並使用這兩個字符串的連接,將搜索已填充的鏈接列表。只要含有具有這些第一名字和第二名字的字符串的節點時,分割該節點,並顯示該節點的結果
我的代碼是:
try {
String fullname;
String mobile;
String home;
String mobile2;
String office;
Node current=first;
while(current.data == null ? key !=null : !current.data.equals(key)) {
String splitter[]=current.next.data.split(" ");
//fullname=splitter[9];
mobile=splitter[1];
home=splitter[2];
mobile2=splitter[3];
office=splitter[4];
if(current.next.data.split(" ")==null?key==null:){
mobilefield.setText(mobile);
homefield.setText(home);
mobilefield2.setText(mobile2);
officefield.setText(office);
} else {
throw new FileNotFoundException("SORRY RECORD NOT LISTED IN DATABASE");
}
break;
}
} catch(Exception e) {
JOptionPane.showMessageDialog(this,e.getMessage()
+"\nPLEASE TRY AGAIN !","Search Error", JOptionPane.ERROR_MESSAGE);
}
的問題是,所有會很好,但對於列表中的第一個和最多第n-1個節點,搜索錯誤,但是在該搜索中沒有到達最後一個節點。
爲什麼不使用標準的java.util.List? –
這個問題的解決方案實際上是在我面前散列圖片而不是鏈接列表。你實際上存儲在一個表中的密鑰,並將實際數據鏈接到密鑰...這就是哈希 – nitgeek