今天看毕老师视频一段复写compare代码有个小小的疑问
毕老师的代码是
public LenComparatot implements Comparator<String>{
public int compare(String s1,String s2){
int num=new Integer(s1.length()).compareTo(new Integer(s2.length())); //此处为什么不写成 int num=s1.length()-s2.length();
if (num==0){
return s1.compareTo(s2);
return num;
}
}
}
个人写成 int num=s1.length()-s2.length();
return num==0?s1.cmpareTo(s2):num;