黑马程序员技术交流社区
标题:
关于TreeSet对比器,只打印一行数据,是咋回事,哪位大神给看看呗
[打印本页]
作者:
范靖明
时间:
2014-8-27 19:30
标题:
关于TreeSet对比器,只打印一行数据,是咋回事,哪位大神给看看呗
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class Student implements Comparator{
private static String name;
private static int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
/*public int compareTo(Object obj){
if(!(obj instanceof Student)){
throw new RuntimeException("haha");
}
Student s = (Student)obj;
if(this.age>s.age){
return 1;
}
if(this.age==s.age){
return 0;
}
return -1;
}*/
public int compare(Object o1,Object o2){
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int num =s1.getName().compareTo(s2.getName());
if(num==0){
if(s1.getAge()>s2.getAge()){
return 1;
}
if(s1.getAge()==s2.getAge()){
return 0;
}
return -1;
}
return num;
}
public static void main(String[] args){
TreeSet ts = new TreeSet(new Student(name, age));
ts.add(new Student("zhangsan",89));
ts.add(new Student("zhangsan",19));
ts.add(new Student("lisi",65));
ts.add(new Student("wangwu",23));
Iterator it = ts.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
System.out.println(s.getName()+"####"+s.getAge());
}
}
}
作者:
王凯路路
时间:
2014-8-27 21:32
TreeSet ts = new TreeSet(new Student(name, age));
请问这句是什么意思 ?
我怎么没记得有这样的构造方法啊 ?
作者:
careit
时间:
2014-8-27 22:53
最基础的java基础都没有掌握
1:类 Student的名字和年龄怎么可以定义为static
private static String name;
private static int age;
2:类 Student应当实现接口Comparable的int compareTo(Object o)方法
而不是接口 Comparator的int compare(Object o1,Object o2)方法;
解决方案:去掉上述 两行 中 的static
这是我的代码
package com.itheima;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class Test10 {
/**
* @param args
* 10、声明类Student,包含3个成员变量:name、age、score,
* 创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题)
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet<Student> treeSet=new TreeSet();
//实例化5个student对象
Student[] stuArr={new Student("赵", 20, 97),
new Student("钱", 22, 84),
new Student("孙", 21, 97),
new Student("李", 23, 89),
new Student("崔", 19, 99)};
//将5个student对象增加到TreeSet对象中
treeSet.add(stuArr[0]);
treeSet.add(stuArr[1]);
treeSet.add(stuArr[2]);
treeSet.add(stuArr[3]);
treeSet.add(stuArr[4]);
Iterator it=treeSet.iterator();
//获取treeSet的遍历器
while(it.hasNext()){
Student p = (Student)it.next();
System.out.println("name = "+p.getName()+" || age = "+p.getAge()+" || age = "+p.getScore());
}
}
static class Student implements Comparable{
//实现Comparable 接口的compareTo(Object o)方法
private String name;
private int age;
private float score;
public Student(String n,int a,float s) {
// TODO Auto-generated constructor stub
name=n;
age=a;
score=s;
}
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 float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
Student stu=(Student) o;
//以下实现的是升序排列
if(this.score>stu.score){
return 1;
}
if(this.score<stu.score){
return -1;
}
return 0;
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2