本帖最后由 sunhaodi 于 2012-6-19 14:40 编辑
package day2;
import java.util.Iterator;
import java.util.TreeSet;
import javax.management.RuntimeErrorException;
public class TestDemo {
public static void main(String[] args) {
TreeSet t = new TreeSet();
t.add(new Test("java10",29));
t.add(new Test("java15",24));
t.add(new Test("java18",57));
t.add(new Test("java20",20));
Iterator it = t.iterator();
while(it.hasNext()){
Test tt=(Test)it.next();
System.out.println(tt.getName()+"........"+tt.getAge());
}
}
}
class Test implements Comparable{
private String name;
private int age;
public Test(String name,int age){
this.name=name;
this.age=age;
}
@Override
public int compareTo(Object o) {
if(!(o instanceof Test))
throw new RuntimeException( "不是Test对象");
Test t=(Test)o;
System.out.println(this.name+".........."+t.name);
if(this.age>t.age)
return 1;
if(this.age==t.age){
return t.name.compareTo(this.name);
}
return -1;
}
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;
}
}
-------------------------------------------
@Override
public int compareTo(Object o) {
if(!(o instanceof Test))
throw new RuntimeException( "不是Test对象");
Test t=(Test)o;
System.out.println(this.name+".........."+t.name);
if(this.age>t.age)
return 1;
if(this.age==t.age){
return t.name.compareTo(this.name);
}
return -1;
这里面怎么倒序排序啊? 还有这是怎么比较的,听毕老师讲完了还是有点没懂,希望有高手解答下
|
|