比较字符串时使用 Comparable 接口

发布于 2024-10-20 02:14:03 字数 666 浏览 2 评论 0原文

我搜索了这个问题,但我只发现了一个有点令人困惑的线索,所以我将在这里询问我希望得到一个更清晰的答案。

我有一个作业,要求使用 Comparable 接口按客户名称对数组中的对象进行排序。到目前为止,我只对整数进行了此操作,因此我不确定如何将字符串进行比较。我该怎么办呢?到目前为止,假设我要使用 a.name 与 this.name 进行比较:

public int comparedTo(Customer a)
{

}   //end comparedTo

我还需要创建一个类来实现 Comparator 接口,以根据客户购买情况对值进行排序,我认为我做得正确,但我想在我拔头发之前确定一下什么时候是错误的。这就是我为此所做的:

class NameComparator implements Comparator{
public int compare(Object cust1, Object cust2){    

    String cust1Purch = ((Customer)cust1).purchase;        
    String cust2Purch = ((Customer)cust2).purchase;

    return cust1Purch.compareTo(cust2Purch);
}

非常感谢任何帮助!

I searched for this question, but I only found one thread that was kind of confusing, so I'm going to ask here for what I hope will be a clearer answer.

I have an assignment to use the Comparable interface to sort objects in an array by customer name. I have only done this with integers so far, so I'm not sure how to compare the strings together. How would I go about that? Here is where I am so far, assuming I am to use a.name compared to this.name:

public int comparedTo(Customer a)
{

}   //end comparedTo

I also need to make a class to implement the Comparator interface to sort the values based on customer purchases and I think I did that properly, but I'd like to make sure before I go ripping my hair out when it's wrong. Here is what I did for that:

class NameComparator implements Comparator{
public int compare(Object cust1, Object cust2){    

    String cust1Purch = ((Customer)cust1).purchase;        
    String cust2Purch = ((Customer)cust2).purchase;

    return cust1Purch.compareTo(cust2Purch);
}

Any help is greatly appreciated!

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

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

发布评论

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

评论(5

尘曦 2024-10-27 02:14:03

一切都可以,但是您可以指定 Comparator 泛型类型,然后无需强制转换对象:

class NameComparator implements Comparator<Customer>{
public int compare(Customer cust1, Customer cust2){    

    String cust1Purch = cust1.purchase;        
    String cust2Purch = cust2.purchase;

    return cust1Purch.compareTo(cust2Purch);
}

Its all ok, but you can specify Comparator generic type and then no need to cast objects:

class NameComparator implements Comparator<Customer>{
public int compare(Customer cust1, Customer cust2){    

    String cust1Purch = cust1.purchase;        
    String cust2Purch = cust2.purchase;

    return cust1Purch.compareTo(cust2Purch);
}
橘香 2024-10-27 02:14:03

这是一个可能对您有所帮助的完整示例:

CustomerComparator

class CustomerComparator implements Comparator<Customer> {

    @Override
    public int compare(Customer c1, Customer c2) {
        return c1.name.compareTo(c2.name);   // or, simply c1.compareTo(c2);
    }
}

Comparable Customer

class Customer implements Comparable<Customer> {

    String name;

    public Customer(String name) {
        this.name = name;
    }

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

    public String toString() {
        return name;
    }
}

一个简单的测试驱动程序:

public class Test {
    public static void main(String[] args) {

        List<Customer> customers = Arrays.asList(new Customer("Bravo"),
                                                 new Customer("Charlie"),
                                                 new Customer("Delta"),
                                                 new Customer("Alpha"));
        Collections.sort(customers);

        // Or
        // Collections.sort(customers, new CustomerComparator());

        System.out.println(customers);

    }
}

(ideone.com 演示)

Here is a complete example that might help you:

A CustomerComparator:

class CustomerComparator implements Comparator<Customer> {

    @Override
    public int compare(Customer c1, Customer c2) {
        return c1.name.compareTo(c2.name);   // or, simply c1.compareTo(c2);
    }
}

A Comparable Customer:

class Customer implements Comparable<Customer> {

    String name;

    public Customer(String name) {
        this.name = name;
    }

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

    public String toString() {
        return name;
    }
}

A simple test driver:

public class Test {
    public static void main(String[] args) {

        List<Customer> customers = Arrays.asList(new Customer("Bravo"),
                                                 new Customer("Charlie"),
                                                 new Customer("Delta"),
                                                 new Customer("Alpha"));
        Collections.sort(customers);

        // Or
        // Collections.sort(customers, new CustomerComparator());

        System.out.println(customers);

    }
}

(ideone.com demo)

酒废 2024-10-27 02:14:03

看起来不错。但你可以使用泛型:

class NameComparator implements Comparator<Customer> {
    public int compare(Customer cust1, Customer cust2) {..}
}

Looks fine. But you can utilize Generics:

class NameComparator implements Comparator<Customer> {
    public int compare(Customer cust1, Customer cust2) {..}
}
も让我眼熟你 2024-10-27 02:14:03

我似乎很适合 Comparable 接口。那里没有什么真正复杂的。

至于Comparator,如果您不使用泛型,则还需要验证同一基类型的两个参数,至少是Comparable,因为您使用的是该接口:

if (cust1 instanceof Comparable && cust2 instanceof Comparable) {
   Comparable c1 = (Comparable) cust1;
   Comparable c2 = (Comparable) cust2;
   return c1.compareTo(c2);
} else {
   return false;
}

I seem to get it right for the Comparable interface. Nothing really complicated there.

As for the Comparator, if you're not using generics, you also need to validate both argument for the same base type, at least Comparable since you're using that interface :

if (cust1 instanceof Comparable && cust2 instanceof Comparable) {
   Comparable c1 = (Comparable) cust1;
   Comparable c2 = (Comparable) cust2;
   return c1.compareTo(c2);
} else {
   return false;
}
老旧海报 2024-10-27 02:14:03

1)我会使用泛型来定义你的比较器并避免额外的类转换:

class NameComparator implements Comparator<Customer> {
    public int compare(Customer cust1, Customer cust2) {
      ...
    }
}

2)java中的String类已经实现了Comparable接口( http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html )。因此,如果您只需要比较客户的姓名或购买字符串,那么您可以将其委托给 String,这就是您已经做的事情。

1) I would use generics to define your comparator and avoid additinal class casting:

class NameComparator implements Comparator<Customer> {
    public int compare(Customer cust1, Customer cust2) {
      ...
    }
}

2) String class in java already implements Comparable interface ( http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html ). So, if you need to just compare on customer's name or purchase string, then you can just delegate it to String and that's what you already do.

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