import java.util.*;
class Person7
{
private String name;
private int age;
Person7(String name,int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
class MyCompare7 implements Comparator
{
public int compare(Object o1,Object o2)
{
Person7 p1 = (Person7)o1;
Person7 p2 = (Person7)o2;
int num = p1.getName().compareTo(p2.getName());
if(num==0)
return new Integer (p1.getAge()).compareTo(new Integer (p2.getAge()));
else
return num;
}
}
class TreeSetDemo7
{
public static void main(String[] args)
{
TreeSet t = new TreeSet(new MyCompare7());
t.add(new Person7("a1",12));
t.add(new Person7("a2",13));
t.add(new Person7("a3",14));
t.add(new Person7("a3",12));
t.add(new Person7("a1",12));
Iterator i = t.iterator();
while(i.hasNext())
{
Person7 p = (Person7)i.next();
System.out.println(p.getName()+"..."+p.getAge());
}
}
}
开始还能运行,,后来不知道怎么就不能运行了,,大家看看,麻烦了哈
TreeSetDemo7.java:20: 错误: 此处需要接口 |