本帖最后由 ninjaes 于 2015-4-28 08:38 编辑
package com.itheima;
import java.util.*;
class test
{
public static void main(String[] args)
{
List <User> al=new ArrayList<User>();
al.add(new User("ll",31));
al.add(new User("Soip",30));
al.add(new User("ou",41));
al.add(new User("oppu",21));
al.add(new User("pou",11));
Collections.sort(al,new Comparator<User>()
{
public int compare(User o1,User o2)
{
return o1.getAge().compareTo(o2.getAge());
}
}
);
for(User u:al)
{
System.out.println(u.getName());
}
}
}
class User
{
private String name;
private Integer age;
User(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public Integer getAge()
{
return age;
}
public void setName(String name)
{
this.name=name;
}
public void setAge(Integer age)
{
this.age=age;
}
}
比较器的方法覆盖没觉得有问题呀 求大神帮我看看 到底是哪里出了问题 |
|