说的有道理!!
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2); //获得两个字符串长度较短的作为while()循环的次数
char v1[] = value;
char v2[] = anotherString.value;
int k = 0; //索引从0开始
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2; //两个字符相减得到的是两个字符相差的ASCII值
}
k++;
}
return len1 - len2;
} |