加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 排序不可比较的元素列表

发布时间:2020-12-15 05:21:09 所属栏目:Java 来源:网络整理
导读:今天我被问到这个面试问题: If I have a Person class with name , age and salary fields,and I put 100 new instances of this Person in an ArrayList ,and then do Collections.sort(list) ,then on what parameter will the list be sorted? 我知道我
今天我被问到这个面试问题:

If I have a Person class with name,age and salary fields,and I put 100 new instances of this Person in an ArrayList,and then do Collections.sort(list),then on what parameter will the list be sorted?

我知道我需要让Person类实现Comparable,然后覆盖compareTo,但如果我不这样做,会发生什么?

解决方法

它不会编译: Collections.sort的1参数版本需要一个可比较的列表.具体地,列表< T>其中T实现了Comparable<?超级T>.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读