本帖最后由 rolan 于 2015-4-30 23:47 编辑
如题,我用的TreeSet ,但是只输出一条信息,是咋回事呢??- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.TreeSet;
- public class Fanxin {
- public static void main(String[] args){
- TreeSet<Person> tre=new TreeSet<Person>(new comp());
- tre.add(new Students("a2",23));
- tre.add(new Students("a1",24));
- tre.add(new Students("a3",20));
- tre.add(new Students("a4",21));
- tre.add(new Students("a5",27));
- Iterator<Person> it=tre.iterator();
- while(it.hasNext()){
- Person p=(Person)it.next();
- System.out.print(p.GetName()+"-------"+p.GetAge());
- }
- }
- }
- class comp implements Comparator<Person>{
- public int compare(Person ob1,Person ob2) {
- return ob1.GetName().compareTo(ob1.GetName());
-
- }
-
- }
- class Person{
- private String name;
- private int age;
- Person(String name,int age){
- this.name=name;
- this.age=age;
- }
- public String GetName(){
- return name;
- }
- public int GetAge(){
- return age;
- }
- }
- class Students extends Person{
- Students(String name, int age) {
- super(name, age);
-
- }
-
- }
- class Workers extends Person{
- Workers(String name, int age) {
- super(name, age);
-
- }
-
- }
复制代码
|
|