Given the code fragments:



and

What is the result?

A.

[A Guide to Java Tour:3.0, Beginning with Java:2.0]

B.

[Beginning with Java:2.0, A Guide to Java Tour:3.0]

C. A compilation error occurs because the Book class does not override the abstract method compareTo().
D. An Exception is thrown at run time.

題解

Book類別實作了Comparator介面,並使用Book物件的name欄位的字串辭典順序來作為排序的依據。

程式第36行,使用了Collections類別的sort方法並傳入Book這個Comparator物件來排序集合物件。

由於A的辭典順序在B的前面,因此排序之後會輸出:

[A Guide to Java Tour:3.0, Beginning with Java:2.0]