[英]Search in a implementation of LinkedList with Comparator
我有這個界面:
class CustomLinkedList<T> {
public void append(T dado);
public void addFirst(T dado);
public T search(Comparator<T> cmp);
public void printObjects();
}
我需要使用比較器在鏈接列表中進行搜索,但是比較器具有帶有2個參數的compare(obj1,obj2)方法。 這是比較器的示例:
public class SearchByEmail implements Comparator<Student> {
public SearchByEmail(String email) {
// TODO Auto-generated constructor stub
}
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return 0;
}
}
有人知道如何使用比較器實現搜索方法嗎?
一個可怕的例子:
public T search(Comparator<T> cmp) {
Node<T> current = first;
do {
if(cmp.compare(current.getElement(), null) == 0){
return current.getElement();
}
current = current.getNext();
} while (current != null);
return null;
}
和SearchByEmail:
public class SearchByEmail implements Comparator<Student> {
private String email;
public SearchByEmail(String email) {
this.email = email;
}
@Override
public int compare(Student o1, Student o2) {
if(o1.getEmail().equals(email)) return 0;
return 1;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.