本帖最后由 何明辉 于 2012-9-9 20:32 编辑
import java.util.*;
class Person implements Comparable
{
private String name;
private int age;
Person(String name,int age)
{
this.name=name;
this.age=age;
}
public int compareTo(Person p)
{
if(this.age>p.age) return 1;
if(this.age==p.age) return 0;
else return -1;
}
public boolean equals(Object obj)
{
Person p=(Person)obj;
if(this.name.equals(p.name)&&this.age==p.age)return true;
else return false;
}
}
clss PersonDemo
{
public static void main(String[] args)
{
Set<Person> st=new TreeSet<Person>();
st.add(new Person("张三",25));
st.add(new Person("李四",26));
st.add(new Person("孙八",27));
st.add(new Person("赵一",21));
st.add(new Person("王二",22));
st.add(new Person("刘三",23));
System.out.println(st);
}
}
不知道程序错哪儿了,请大家帮我看看,谢谢! |