package sgsh;
import java.util.*;
public class Paixu
{
public static void main(String[] args)
{
ArrayList<User> ts = new ArrayList<User>();
ts.add(new User("张三",22));
ts.add(new User("李四",20));
ts.add(new User("王五",19));
ts.add(new User("赵六",23));
Iterator<User> it = ts.iterator();
while(it.hasNext())
{
User us= (User)it.next();
System.out.println(us.getName()+"..."+us.getAge());
}
}
}
class User implements Comparable<Object>
{
private String name;
private int age;
User(String name,int age)
{
this.name = name;
this.age = age;
}
public int compareTo(Object obj)
{
User s=(User)obj;
return this.getAge()-s.getAge();
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
继承comparable 问什么输出没有排序 |
|