如何在 Java 中的字符串上使用 Comparable CompareTo

发布于 2024-09-24 06:30:29 字数 329 浏览 0 评论 0 原文

我可以用它按 emp id 排序,但我不确定是否可以比较字符串。我收到错误:字符串的运算符未定义。

public int compareTo(Emp i) {
            if (this.getName() == ((Emp ) i).getName())
                return 0;
            else if ((this.getName()) > ((Emp ) i).getName())
                return 1;
            else
                return -1;

I can use it to sort by emp id but I'm not sure if it is possible to compare strings. I get an error the operator is undefined for strings.

public int compareTo(Emp i) {
            if (this.getName() == ((Emp ) i).getName())
                return 0;
            else if ((this.getName()) > ((Emp ) i).getName())
                return 1;
            else
                return -1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

终难遇 2024-10-01 06:30:29

您需要使用的是字符串的 compareTo() 方法。

return this.getName().compareTo(i.getName());

那应该做你想做的事。

通常,在实现Comparable接口时,您只需组合使用该类的其他Comparable成员的结果。

下面是一个比较典型的compareTo()方法的实现:

class Car implements Comparable<Car> {
    int year;
    String make, model;
    public int compareTo(Car other) {
        if (!this.make.equalsIgnoreCase(other.make))
            return this.make.compareTo(other.make);
        if (!this.model.equalsIgnoreCase(other.model))
            return this.model.compareTo(other.model);
        return this.year - other.year;
    }
}

What you need to use is the compareTo() method of Strings.

return this.getName().compareTo(i.getName());

That should do what you want.

Usually when implementing the Comparable interface, you will just combine the results of using other Comparable members of the class.

Below is a pretty typical implementation of a compareTo() method:

class Car implements Comparable<Car> {
    int year;
    String make, model;
    public int compareTo(Car other) {
        if (!this.make.equalsIgnoreCase(other.make))
            return this.make.compareTo(other.make);
        if (!this.model.equalsIgnoreCase(other.model))
            return this.model.compareTo(other.model);
        return this.year - other.year;
    }
}
初见终念 2024-10-01 06:30:29

很确定你的代码可以这样写:

public int compareTo(Emp other)
{
    return this.getName().compareTo(other.getName());
}

Pretty sure your code can just be written like this:

public int compareTo(Emp other)
{
    return this.getName().compareTo(other.getName());
}
GRAY°灰色天空 2024-10-01 06:30:29

Java String 已经实现了 Comparable。因此,您可以简单地将方法编写为

public int compareTo(Emp emp) {
   return this.getName().compareTo(emp.getName());
}

(当然,请确保添加适当的验证,例如空检查等)。

另外,在代码中,不要尝试使用“==”来比较字符串。请改用“等于”方法。 '==' 仅比较字符串引用,而 equals 在语义上比较两个字符串。

Java String already implements Comparable. So you could simply write your method as

public int compareTo(Emp emp) {
   return this.getName().compareTo(emp.getName());
}

(ofcourse make sure you add proper validations such as null checks etc)

Also in your code, do not try to compare Strings using '=='. Use 'equals' method instead. '==' only compare string references while equals semantically compares two strings.

征﹌骨岁月お 2024-10-01 06:30:29

你不需要将 i 转换为 Emp,它已经是一个 Emp:

public int compareTo(Emp i) {
    return getName().compareTo(i.getName());
}

You don't need to cast i to Emp, it's already an Emp:

public int compareTo(Emp i) {
    return getName().compareTo(i.getName());
}
淡写薰衣草的香 2024-10-01 06:30:29

不应该是

if (this.getName() == ((Emp ) i).getName())

if

(this.getName().equals(i.getName()) )

Shouldn't

if (this.getName() == ((Emp ) i).getName())

be

if (this.getName().equals(i.getName()))

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