import java.util.*;
class User implements Comparable
{
private String name;
private int age;
User(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 User )){
throw new RuntimeException(" 不是User类对象");
}
User s=(User)obj;
if(this.age<s.age)
return 1;
if(this.age==s.age)
return this.name.compareTo(s.name);
return -1;
}
}
public class test{
public static void main(String[] args){
ArrayList<User> us=new ArrayList<User>();
us.add(new User("李洋",19));
us.add(new User("李新",18));
us.add(new User("李雨",20));
us.add(new User("李海",17));
us.add(new User("李墨",26));
Iterator<User> it=us.iterator();
while(it.hasNext()){
User lx=(User)it.next();
System.out.println(lx);
}
}
}
运行时报错,求大神解答 |
|