0

我想使用 Java SE 制作一个带有链表的电话簿原型项目。我需要存储名字、姓氏、手机、家庭和办公室等数据。

LinkedList实际上,我想知道如何从using中搜索此类数据

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;

}
4

1 回答 1

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);
}
于 2012-12-28T10:51:27.200 回答