Given:



class CheckClass {

    public static int checkValue(String s1, String s2) {
        return s1.length() - s2.length();
    }
}

and the code fragment:

String[] strArray = new String[]{"Tiger", "Rat", "Cat", "Lion"};
//line n1
for (String s : strArray) {
    System.out.print(s + " ");
}

Which code fragment should be inserted at line n1 to enable the code to print Rat Cat Lion Tiger ?

A.

Arrays.sort(strArray, CheckClass::checkValue);
B.
Arrays.sort(strArray, (CheckClass::new)::checkValue);
C.
Arrays.sort(strArray, (CheckClass::new).checkValue);
D.
Arrays.sort(strArray, CheckClass::new::checkValue);

題解

Arrays類別的sort方法可以排序傳入的陣列,也可以再傳入Comparator來自行決定陣列的排序方式。CheckClass類別的checkValue方法是類別方法,不需要實體化即可使用,因此答案是選項A。