2012-12-28 93 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); 
}