
treeset定制排序使用什么设计模式
1个回答
2016-09-22
展开全部
treeset定制排序使用什么设计模式
例: 对加入 TreeSet的Student对象 实现Comparable接口,通过在Student中重写compareTo()方法确定该对象在TreeSet中的排序方式 Student类如下 class Student implements Comparableprivate int age;//重写compareTo()方法public int compareTo(Object o){Student s = (Student) o;if (this.age < s.age )return -1;else if(s.age == this.age)return 0;elsereturn 1;}当Student对象加入TreeSet时,是按年龄从小到大排序的。
例: 对加入 TreeSet的Student对象 实现Comparable接口,通过在Student中重写compareTo()方法确定该对象在TreeSet中的排序方式 Student类如下 class Student implements Comparableprivate int age;//重写compareTo()方法public int compareTo(Object o){Student s = (Student) o;if (this.age < s.age )return -1;else if(s.age == this.age)return 0;elsereturn 1;}当Student对象加入TreeSet时,是按年龄从小到大排序的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询