周坤 发表于 2012-8-7 12:12
错误貌似多了,最好自己再熟悉熟悉。
太谢谢你给我改代码了
我又改了改
代码么有错误了,可是现在只能存进去两个对象,为什么?- import java.util.*;
- class Person
- {
- private String name;
- private int age;
- Person(String name, int age)
- {
- this.name=name;
- this.age=age;
- }
- public int getAge()
- {
- return age;
- }
- public String getName()
- {
- return name;
- }
- }
- class Mycom implements Comparator<Person>
- {
- public int compare(Person a ,Person b)
- {
- int c = new Integer(a.getAge()).compareTo(new Integer (b.getAge()));
- if (c==0)
- {
- return a.getName().compareTo(b.getName());
- }
- return c ;
- }
- }
- class TreeSetDemo2
- {
- public static void main(String[] args)
- {
- TreeSet<Person> ts = new TreeSet<Person>(new Mycom());
- ts.add(new Person("dafsd",13));
- ts.add(new Person("sda",12));
- ts.add(new Person("weqd",14));
- ts.add(new Person("dawsd",17));
- Iterator<Person> it = ts.iterator();
- while (it.hasNext())
- {
- System.out.println(it.next().getName()+"-----"+it.next().getAge());
- }
- }
- }
复制代码 |