A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© haio 中级黑马   /  2014-3-10 14:02  /  1169 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 haio 于 2014-3-10 22:03 编辑

import java.util.*;
class Demo {
       public static void main (String [] args)
{
  //创建一个TreeSet集合;并添加对象;
      TreeSet<Student> ts = new TreeSet<Student>();
      ts.add(new Student("zhangsan1",20,90));
      ts.add(new Student("zhangsan2",19,95));
      ts.add(new Student("zhangsan3",20,90));
      ts.add(new Student("zhangsan4",21,98));
      ts.add(new Student("zhangsan5",22,97));
  //对集合中的元素进行获取,
      Iterator<Student> it = ts.iterator();
      while(it.hasNext()){
   //对获取的对象进行强制转换;
       Student stu = it.next();
       System.out.println(stu.getName()+stu.getAge()+stu.getScore());
      }/**/
}

}
//创建一个类并实现Comparable接口
class Student implements Comparable
{
     private String name;
     private int age;
     private int score;
  Student(String name,int age,int score){
     this.name=name;
     this.age=age;
     this.score=score;
   }
     public String getName(){
     return name;
    }
    public void setName(String name){
    this.name=name;
   }
   public int getAge(){
   return age;
   }
   public void setAge(int age){
   this.age=age;
   }
  public int getScore(){
  return score;
  }
public void setScore(int score){
  this.score=score;
}
public int compareTo(Object obj)
{
  Student s=(Student)obj;
  if(this.getScore()>s.getScore())
  return 1;
  if(this.getScore()==s.getScore()){
   return this.getName().compareTo(s.getName());
  }
  return -1;
}

}

评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

9 个回复

倒序浏览
Compatable也是需要加泛型的了,你加下泛型试试看看可以吗

评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

回复 使用道具 举报
class Student implements Comparable<Student>,

我们自定义一个类进行排序,那么必须对对象制定好排序规则,并且,每个对象所在的类都必须实现Comparable接口(这种方式也称为元素的自然顺序,或者叫做默认顺序)才可以使用.这是最起码的条件。然后你得告诉它什么的对象要排序或者比较才行{:3_59:}

评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

回复 使用道具 举报
嗯,谢谢!!
回复 使用道具 举报
centian2005 发表于 2014-3-10 18:10
class Student implements Comparable,

我们自定义一个类进行排序,那么必须对对象制定好排序规则,并且, ...

不行啊,还是错误
回复 使用道具 举报
无道 发表于 2014-3-10 15:21
Compatable也是需要加泛型的了,你加下泛型试试看看可以吗

不行啊??
回复 使用道具 举报
亮灯并不是错误,这里亮灯原因也确实是因为没有加泛型,但这里泛型应该定义成Object类型
  1. class Student implements Comparable<Object>
复制代码

因为你下面public int compareTo(Object obj)这个方法接收的参数是Object类型的,或者两个地方都改成Student类型也可以,可以明确指出是为Student类定义排序规则

评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

回复 使用道具 举报
无道 中级黑马 2014-3-12 09:53:57
8#

把全部代码贴上来,
回复 使用道具 举报
haio 中级黑马 2014-3-12 19:17:54
9#
无道 发表于 2014-3-12 09:53
把全部代码贴上来,

都在上面了
回复 使用道具 举报
Comparable是一个可以被泛型修饰的接口:

当你没有明确指定泛型的时候:
这个接口要被实现的方法默认为compareTo(Object o)
例如:
  1. class Test implements Comparable{
  2.         @Override
  3.         public int compareTo(Object o) {
  4.                 return 0;
  5.         }       
  6. }
复制代码


当你的Comparable加上泛型修饰之后,compareTo方法的入参类型将随着你指定泛型的变化而变化
例如:
  1. class Test implements Comparable<String>{
  2.         @Override
  3.         public int compareTo(String o) {
  4.                 // TODO Auto-generated method stub
  5.                 return 0;
  6.         }       
  7. }
复制代码


当然,你也可以保留原来泛型的形式。
例如:
  1. class Test<T> implements Comparable<T>{
  2.         @Override
  3.         public int compareTo(T o) {
  4.                 // TODO Auto-generated method stub
  5.                 return 0;
  6.         }       
  7. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马