Java compareTo方法。student.compareTo方法应返回
我被要求编写一个student.compareTo方法,如果两个学生的名字和姓氏相同,该方法应返回0。如果学生的名字按字典顺序排序低于传入的名字,那么它应该返回负值。如果学生的名字在词典中的排序高于传入的名字,那么它应该返回一个正值 这是到目前为止我的代码。对于负值和正值,应该是固定值还是使用比较值Java compareTo方法。student.compareTo方法应返回,java,string,compareto,Java,String,Compareto,我被要求编写一个student.compareTo方法,如果两个学生的名字和姓氏相同,该方法应返回0。如果学生的名字按字典顺序排序低于传入的名字,那么它应该返回负值。如果学生的名字在词典中的排序高于传入的名字,那么它应该返回一个正值 这是到目前为止我的代码。对于负值和正值,应该是固定值还是使用比较值 public int compareTo(Student){ int comparison = (this.firstName.compareTo(Student.firstName)); int
public int compareTo(Student){
int comparison = (this.firstName.compareTo(Student.firstName));
int comparison2 = (this.lastName.compareTo(Student.lastName));
if (comparison == comparison2)
return 0;
else if ((comparison=0 && comparison2<0) ||(comparison<0 && comparison2=0)
return -1;
else
return 1;
}
这太复杂了 连锁你的比较;仅当第一个返回0时,运行第二个;如果第二个返回0,则运行第三个;等等 也就是说,返回第一个非零比较结果或最后一个。例如:
@Override
public int compareTo(final Student other)
{
int ret = firstName.compareTo(other.firstName);
if (ret != 0) // No need to go further
return ret;
// Hypothetic second comparison to do before lastName
// ret = foo.compareTo(other.foo);
// if (ret != 0)
// return ret;
// Rinse, repeat...
// All previous comparisons returned 0,
// return the result of the last comparison
return lastName.compareTo(other.lastName);
}
番石榴对此有一个很好的解释:
@Override
public int compareTo(final Student other)
{
return ComparisonChain.start()
.compare(firstName, other.firstName)
.compare(lastName, other.lastName)
.result();
}
这太复杂了 连锁你的比较;仅当第一个返回0时,运行第二个;如果第二个返回0,则运行第三个;等等 也就是说,返回第一个非零比较结果或最后一个。例如:
@Override
public int compareTo(final Student other)
{
int ret = firstName.compareTo(other.firstName);
if (ret != 0) // No need to go further
return ret;
// Hypothetic second comparison to do before lastName
// ret = foo.compareTo(other.foo);
// if (ret != 0)
// return ret;
// Rinse, repeat...
// All previous comparisons returned 0,
// return the result of the last comparison
return lastName.compareTo(other.lastName);
}
番石榴对此有一个很好的解释:
@Override
public int compareTo(final Student other)
{
return ComparisonChain.start()
.compare(firstName, other.firstName)
.compare(lastName, other.lastName)
.result();
}
这太复杂了 连锁你的比较;仅当第一个返回0时,运行第二个;如果第二个返回0,则运行第三个;等等 也就是说,返回第一个非零比较结果或最后一个。例如:
@Override
public int compareTo(final Student other)
{
int ret = firstName.compareTo(other.firstName);
if (ret != 0) // No need to go further
return ret;
// Hypothetic second comparison to do before lastName
// ret = foo.compareTo(other.foo);
// if (ret != 0)
// return ret;
// Rinse, repeat...
// All previous comparisons returned 0,
// return the result of the last comparison
return lastName.compareTo(other.lastName);
}
番石榴对此有一个很好的解释:
@Override
public int compareTo(final Student other)
{
return ComparisonChain.start()
.compare(firstName, other.firstName)
.compare(lastName, other.lastName)
.result();
}
这太复杂了 连锁你的比较;仅当第一个返回0时,运行第二个;如果第二个返回0,则运行第三个;等等 也就是说,返回第一个非零比较结果或最后一个。例如:
@Override
public int compareTo(final Student other)
{
int ret = firstName.compareTo(other.firstName);
if (ret != 0) // No need to go further
return ret;
// Hypothetic second comparison to do before lastName
// ret = foo.compareTo(other.foo);
// if (ret != 0)
// return ret;
// Rinse, repeat...
// All previous comparisons returned 0,
// return the result of the last comparison
return lastName.compareTo(other.lastName);
}
番石榴对此有一个很好的解释:
@Override
public int compareTo(final Student other)
{
return ComparisonChain.start()
.compare(firstName, other.firstName)
.compare(lastName, other.lastName)
.result();
}