本帖最后由 唐志海 于 2014-1-21 02:38 编辑
帮忙看下面的代码哪里出错了。。会发生NoSuchElementException异常
- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.TreeSet;
- public class fanXingTest2
- {
- public static void main(String[] args)
- {
- TreeSet<Student> ts=new TreeSet<Student>(new comp());
- ts.add(new Student("zhangsan",11));
- ts.add(new Student("lishi3",14));
- ts.add(new Student("lishi3",11));
- ts.add(new Student("lishi2",11));
- ts.add(new Student("lishi1",12));
- printl(ts);
- }
- public static void printl(TreeSet<? extends Person> ts)
- {
- Iterator<? extends Person> it=ts.iterator();
- while(it.hasNext())
- {
- System.out.println(it.next().getName()+"....."+it.next().getAge());
- }
- }
- }
- class comp implements Comparator<Person>
- {
- public int compare(Person p1,Person p2)
- {
-
- int num=p1.getName().compareTo(p2.getName());
- if(num==0)
- {
- return new Integer(p1.getAge()).compareTo(new Integer(p2.getAge()));
-
- }
- return num;
- }
- }
- class Person
- {
- private String name;
- private int age;
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getAge() {
- return age;
- }
- Person(String name,int age)
- {
- this.name=name;
- this.age=age;
- }
-
- }
- class Student extends Person
- {
- Student(String name,int age)
- {
- super(name,age);
-
- }
-
- }
复制代码 |