比较两个通用对象的问题
我的项目是一个使用 BSTree
的电话簿。树的每个节点都是BTNode
。在主类中,当我定义节点时,我将 E 替换为 Pair 类,该类具有 (String name, String number)
。
我有以下比较器类来比较 2 个 E 类型:
import java.util.Comparator;
public class BTNodeComparator<E> implements Comparator<E>
{
public int compare(E a, E b) throws ClassCastException
{
return ((Comparable<E>) a).compareTo(b);
}
}
并且我在 BSTree
中使用它。
现在,当我运行程序并进入比较器时,它在比较器处给我错误,因为现在它在两对之间进行比较
我该如何解决这个问题?我想要的是比较两对的名字?
抱歉我的解释不好。我的英语很弱。
=========================
编辑:
@Tim:尝试你的解决方案后,它给了我这个错误:
java.lang.NullPointerException
at Pair.compareTo(Pair.java:36) // @ return name.compareTo(pair.getName());
at Pair.compareTo(Pair.java:2) // @ public class Pair implements Comparable<Pair>
at BTNodeComparator.compare(BTNodeComparator.java:24) // @ return (a.compareTo(b));
at BTNodeComparator.compare(BTNodeComparator.java:20) // @ public class BTNodeComparator<E extends Comparable<E>> implements Comparator<E>
at BSTree.search(BSTree.java:285)
at BSTree.insert(BSTree.java:300)
at PhoneBook.main(PhoneBook.java:25)
顺便说一句,我在 BSTree 中声明了 BTNodeComparator,如下所示:
protected Comparator<E> c = new BTNodeComparator();
if (c.compare(target, cursor.getElement()) < 0) cursor = cursor.getLeft();
My project is a Phonebook that is using BSTree<E>
. Each node of the tree is BTNode<E>
. In the main class, I replace E with Pair class, which has (String name, String number)
, when I define the nodes.
I have the following comparator class to compare between 2 E types:
import java.util.Comparator;
public class BTNodeComparator<E> implements Comparator<E>
{
public int compare(E a, E b) throws ClassCastException
{
return ((Comparable<E>) a).compareTo(b);
}
}
and I use it in BSTree<E>
.
Now, when I run the program and it comes to the comparator, it gives me errors at the comparator because now it compares between two Pairs
How can I solve this issue? What I want is to compare between the names of the two pairs ?
Sorry for my bad explanation. My English is weak.
=========================
Edit:
@Tim: After trying your solution it gives me this error:
java.lang.NullPointerException
at Pair.compareTo(Pair.java:36) // @ return name.compareTo(pair.getName());
at Pair.compareTo(Pair.java:2) // @ public class Pair implements Comparable<Pair>
at BTNodeComparator.compare(BTNodeComparator.java:24) // @ return (a.compareTo(b));
at BTNodeComparator.compare(BTNodeComparator.java:20) // @ public class BTNodeComparator<E extends Comparable<E>> implements Comparator<E>
at BSTree.search(BSTree.java:285)
at BSTree.insert(BSTree.java:300)
at PhoneBook.main(PhoneBook.java:25)
BTW, I decleared BTNodeComparator in BSTree as follows:
protected Comparator<E> c = new BTNodeComparator();
if (c.compare(target, cursor.getElement()) < 0) cursor = cursor.getLeft();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能看到的唯一可能的失败是ClassCastException。
要修复代码,您需要像这样指定
Pair
类:一般来说,如果您的
Comparator
实现依赖于实现Comparable
的泛型类型那么你应该这样指定它:这样做只会为你提供编译时安全性,而不会改变Comparator如何工作的语义。
根据您的评论,我猜测您正在将
BTNodeComparator
作为嵌套内部类嵌入到BSTree
类中,这意味着您可能无法在没有更改 BSTree 声明中的类型定义。The only possible failure I could see is
ClassCastException
.To fix your code you will need to specify your
Pair
class like this:In general, if your
Comparator
implementation relies on the generic type implementingComparable
then you should specify it like this:Doing so just gives you compile time safety without changing the semantics of how the
Comparator
works.Based on your comment, I'm guessing you are embedding your
BTNodeComparator
into theBSTree
class as a nested inner-class, which means you probably can't make that change without changing the type definition in theBSTree
declaration.