比较两个通用对象的问题

发布于 2024-11-05 12:47:10 字数 1492 浏览 0 评论 0原文

我的项目是一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲸落 2024-11-12 12:47:10

我能看到的唯一可能的失败是ClassCastException。
要修复代码,您需要像这样指定 Pair 类:

class Pair implements Comparable<Pair> {
    String name;
    String number;

    //some implementation stuff...

    @Override
    public int compareTo(Pair o) {
        return name.compareTo(o.name);
    }
}

一般来说,如果您的 Comparator 实现依赖于实现 Comparable 的泛型类型那么你应该这样指定它:

public class BTNodeComparator<E extends Comparable<E>> implements Comparator<E> {
    public int compare(final E a, final E b) {
        return a.compareTo(b);
    }
}

这样做只会为你提供编译时安全性,而不会改变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:

class Pair implements Comparable<Pair> {
    String name;
    String number;

    //some implementation stuff...

    @Override
    public int compareTo(Pair o) {
        return name.compareTo(o.name);
    }
}

In general, if your Comparator implementation relies on the generic type implementing Comparable then you should specify it like this:

public class BTNodeComparator<E extends Comparable<E>> implements Comparator<E> {
    public int compare(final E a, final E b) {
        return a.compareTo(b);
    }
}

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 the BSTree class as a nested inner-class, which means you probably can't make that change without changing the type definition in the BSTree declaration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文