CompareTo(对象o)用于在java中比较字符串
在我的程序中,我试着用姓氏来比较名字,如果它们是相同的,那么就用名字来比较。但是,我不太明白如何比较字符串 有人能帮我解决这个问题吗CompareTo(对象o)用于在java中比较字符串,java,string,object,compareto,Java,String,Object,Compareto,在我的程序中,我试着用姓氏来比较名字,如果它们是相同的,那么就用名字来比较。但是,我不太明白如何比较字符串 有人能帮我解决这个问题吗 public class Student implements IComparable { String firstName; String lastName; int score; public Student() { } public void setFirstName(String firstName) { this.firstName = f
public class Student implements IComparable
{
String firstName;
String lastName;
int score;
public Student()
{
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getFirstName()
{
return firstName;
}
public void getLastName(String lastName)
{
this.lastName = lastName;
}
public String getLastName()
{
return lastName;
}
public void getScore(int score)
{
this.score = score;
}
public int getScore()
{
return score;
}
@Override
public int compareTo(Object o)
{
//Compares Student objects by last name. If the last names are the same
//it compares by first name.
Student s = (Student) o;
if (this.getLastName().toUpperCase() < s.getLastName().toUpperCase())
return -1;
else if (this.getLastName().toUpperCase() > s.getLastName().toUpperCase())
return 1;
else
{
if(this.getFirstName().toUpperCase( < s.getFirstName().toUpperCase()
return -1;
else if (this.getFirstName().toUpperCase( > s.getFirstName().toUpperCase()
return 1;
else
return 0;
}
}
}
公共类学生实现IComparable
{
字符串名;
字符串lastName;
智力得分;
公立学生()
{
}
public void setFirstName(字符串firstName)
{
this.firstName=firstName;
}
公共字符串getFirstName()
{
返回名字;
}
public void getLastName(字符串lastName)
{
this.lastName=lastName;
}
公共字符串getLastName()
{
返回姓氏;
}
公共无效getScore(整数分数)
{
这个分数=分数;
}
公共整数getScore()
{
返回分数;
}
@凌驾
公共整数比较对象(对象o)
{
//按姓氏比较学生对象。如果姓氏相同
//它以名字来比较。
学生s=(学生)o;
if(this.getLastName().toUpperCase()s.getLastName().toUpperCase())
返回1;
其他的
{
if(this.getFirstName().toUpperCase(s.getFirstName().toUpperCase())
返回1;
其他的
返回0;
}
}
}
不要让事情变得更复杂:
类已经提供了String
方法compareTognoreCase
的比较方法返回的值已经可以直接返回了String
int compare = getLastName().compareToIgnoreCase(o.getLastName());
return compare == 0 ? getFirstName().compareToIgnoreCase(o.getFirstName()) : compare;
请注意,如果您有一个对象
参数,则需要检查o学生实例
我不明白为什么要使用自定义的
IComparable
接口,它听起来很像C#中提供的接口,因为Java提供了compariable
,它是通用的,不需要检查参数的运行时类型(因为它不再是对象,而是t
).不要让事情变得更复杂:
String
类已经提供了compareTognoreCase
方法
String
的比较方法返回的值已经可以直接返回了
基本上相同的功能可以通过以下方式表达:
int compare = getLastName().compareToIgnoreCase(o.getLastName());
return compare == 0 ? getFirstName().compareToIgnoreCase(o.getFirstName()) : compare;
请注意,如果您有一个对象
参数,则需要检查o学生实例
我不明白为什么要使用自定义的IComparable
接口,它听起来很像C#中提供的接口,因为Java提供了compariable
,它是通用的,不需要检查参数的运行时类型(因为它不再是对象,而是t
).什么是IComparable
?在Java中,compariable
是通用的。什么是IComparable
?在Java中,compariable
是通用的。instanceof已经做了空检查,如果为空则返回false如果你检查o学生的instanceof
,你也不需要检查o!=null
,因为实例of
对于null
,总是返回false
。谢谢。这帮了大忙。我忘记了字符串可以用那种方式进行比较,所以事情太复杂了。instanceof已经做了null检查,如果为null则返回false如果你检查o学生的instanceof
,你也不需要检查o!=null
,s因为instanceof
对于null
总是返回false
。谢谢你。这帮了大忙。我太复杂了,因为我忘了字符串可以这样比较。