2012-12-28 141 views
0

我想使用Java SE製作帶有鏈接列表的電話簿原型項目。 我需要存儲數據,如名字,姓氏,手機,家庭和辦公室。搜索鏈接列表

其實,我想知道我怎麼可以從一個LinkedList使用

public Node search(String key){ 

    Node current=first; 

    while(current.data == null ? key != null : !current.data.equals(key)) 
     if(current.next==null) 
      return null; 
     else 
      current=current.next; 
     return current; 

} 
+2

爲什麼你不想使用地圖? –

+3

這裏有一個提示:如果你是java noob不使用三元功夫 – Bohemian

+0

LinkedList定義在哪裏? –

回答

0

我不會寫我自己的LinkedList搜索這種類型的數據,但假設這是功課我會寫像這樣。

public Node search(String key){ 
    for(Node n = first; n != null; n = n.next) 
     if(isEqu(n.data, key)) 
      return n; 
    return null; 
} 

private static boolean isEqu(Object o1, Object o2) { 
    return o1 == null ? o2 == null : o1.equals(o2); 
}