比较字符串时使用 Comparable 接口
我搜索了这个问题,但我只发现了一个有点令人困惑的线索,所以我将在这里询问我希望得到一个更清晰的答案。
我有一个作业,要求使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一切都可以,但是您可以指定 Comparator 泛型类型,然后无需强制转换对象:
Its all ok, but you can specify Comparator generic type and then no need to cast objects:
这是一个可能对您有所帮助的完整示例:
CustomerComparator
:Comparable
Customer
:一个简单的测试驱动程序:
(ideone.com 演示)
Here is a complete example that might help you:
A
CustomerComparator
:A
Comparable
Customer
:A simple test driver:
(ideone.com demo)
看起来不错。但你可以使用泛型:
Looks fine. But you can utilize Generics:
我似乎很适合 Comparable 接口。那里没有什么真正复杂的。
至于
Comparator
,如果您不使用泛型,则还需要验证同一基类型的两个参数,至少是Comparable
,因为您使用的是该接口: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 leastComparable
since you're using that interface :1)我会使用泛型来定义你的比较器并避免额外的类转换:
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:
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.