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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马田冬雪 中级黑马   /  2013-2-28 17:25  /  1442 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSet {
        public static void main(String[] args) {
                TreeSet<Student> ts = new TreeSet<Student>();

                Student s1 = new Student("李白", 28);
                Student s2 = new Student("杜甫", 25);
                Student s3 = new Student("李白", 28);
                Student s4 = new Student("白居易", 23);
                Student s5 = new Student("杜甫", 25);
                Student s6 = new Student("李清照", 25);
                Student s7 = new Student("李照", 35);
                Student s8 = new Student("李", 27);

                ts.add(s1);
                ts.add(s2);
                ts.add(s3);
                ts.add(s4);
                ts.add(s5);
                ts.add(s6);
                ts.add(s7);
                ts.add(s8);

                Iterator<Student> it = ts.iterator();
                while (it.hasNext()) {
                        Students s = it.next();
                        System.out.println(s.getName() + "***" + s.getAge());
                }
        }
}

3 个回复

倒序浏览
首先你代码不全,把 class Student的代码也贴出来
不能直接定义TreeSet class 这样就掩盖了Java.util.TreeSet
回复 使用道具 举报
书写错误:
类名不可以写成treeSet   TreeSet是应用类     类名换一个

while循环中  Students s = it.next();
student写错了   

方法错误:
TreeSet是依靠TreeMap来实现的。
TreeSet是一个有序集合,TreeSet中的元素将按照升序排列,缺省是按照自然排序进行排列,意味着TreeSet中的元素要实现Comparable接口。或者有一个自定义的比较器。
我们可以在构造TreeSet对象时,传递实现Comparator接口的比较器对象。


评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1

查看全部评分

回复 使用道具 举报
TreeSet实例   你运行一下 看看    不懂再提

//创建学生类的对象,作为TreeSet的元素
public class Student {
private String name;
    private int score;
    public Student(String name,int score){
     this.name=name;
     this.score=score;
    }
    public String getName(){
     return name;
    }
    public void setName(String name){
     this.name=name;
    }
    public int getScore(){
     return score;
    }
    public void setScore(int score){
     this.score=score;
    }
}




//本类作为比较器,指定比较方法
import java.util.*;
public class ComparaScore implements Comparator<Student>{
  public int compare(Student stu1,Student stu2){
   if(stu1.getScore()<stu2.getScore())
       return 1;
   if(stu1.getScore()>stu2.getScore())
    return -1;
   return 0;
  }
  public boolean equals(Object obj){
   return super.equals(obj);
   
  }
}




//从键盘输入学生姓名和成绩,降序排列后输出
import java.util.*;
import java.io.*;
public class demoTreeSet {
    public static void main(String[] args) {
  Scanner in=new Scanner(System.in);
  ComparaScore comp=new ComparaScore();
  //用comp指定的比较方法,创建一棵红黑树
  TreeSet<Student> stuTree=new TreeSet<Student>(comp);
  String name;
  Integer score;
  System.out.println("please input students' name and score by turns:");
  boolean goon=true;
  while(goon){//循环输入学生姓名和成绩
   System.out.println("input name:");
   name=in.nextLine();
   if(name.length()>0){
                System.out.println("input score");
                score=new Integer(in.nextLine());
                stuTree.add(new Student(name,score));//插入到数中,它会自动排序
   }
   else
    goon=false;
          }
  in.close();
  System.out.println("学生成绩按降序排列:");//用迭代器来遍历这棵树
  Iterator it=stuTree.iterator();
  while(it.hasNext()){
   Student st=(Student)it.next();
   System.out.println("name"+st.getName()+"score"+st.getScore());
  }
    }  
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马