在String.CASE_INSENSITIVE_ORDER.compare(String,String)中,函数compare(String,String)定义在哪里?

时间:2015-11-11 04:09:53

标签: android

在String.CASE_INSENSITIVE_ORDER.compare(String,String)中,函数compare(String,String)定义在哪里?

In String.CASE_INSENSITIVE_ORDER.compare(String, String), where is the function compare(String, String) defined?

2 个答案:

答案 0 :(得分:1)

imgimg { width: 20%; }CASE_INSENSETIVE_ORDER类的静态字段。 它包含String方法

请参阅this

答案 1 :(得分:1)

String.CASE_INSENSITIVE_ORDER是比较器接口的实现。这是在String类中实现的。这是String.java文件中的整个Comparator。

public static final Comparator<String> CASE_INSENSITIVE_ORDER
                                     = new CaseInsensitiveComparator();
private static class CaseInsensitiveComparator
        implements Comparator<String>, java.io.Serializable {
    // use serialVersionUID from JDK 1.2.2 for interoperability
    private static final long serialVersionUID = 8575799808933029326L;

    public int compare(String s1, String s2) {
        int n1 = s1.length();
        int n2 = s2.length();
        int min = Math.min(n1, n2);
        for (int i = 0; i < min; i++) {
            char c1 = s1.charAt(i);
            char c2 = s2.charAt(i);
            if (c1 != c2) {
                c1 = Character.toUpperCase(c1);
                c2 = Character.toUpperCase(c2);
                if (c1 != c2) {
                    c1 = Character.toLowerCase(c1);
                    c2 = Character.toLowerCase(c2);
                    if (c1 != c2) {
                        // No overflow because of numeric promotion
                        return c1 - c2;
                    }
                }
            }
        }
        return n1 - n2;
    }
}