今天我被问到这个面试问题:
If I have a
Person
class withname
,age
andsalary
fields, and I put 100 new instances of thisPerson
in anArrayList
, and then doCollections.sort(list)
, then on what parameter will the list be sorted?
我知道我需要让 Person
类实现 Comparable
然后覆盖 compareTo
,但如果我不这样做,会发生什么?
最佳答案
它不会编译: Collections.sort
的 1 参数版本需要一个 Comparable
的列表秒。具体来说,一个 List<T>
其中 T
工具 Comparable<? super T>
.
关于java - 对不可比较元素列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17432489/