黑马程序员技术交流社区

标题: 关于TreeSet的问题 [打印本页]

作者: 从前有个人    时间: 2013-11-18 00:38
标题: 关于TreeSet的问题
本帖最后由 从前有个人 于 2013-11-18 11:57 编辑

package TreeSet;
import java.util.*;
class TreeSetTest {
public static void main(String[] args) {
  
  TreeSet ts = new TreeSet(); //在这挂掉了
  ts.add(new student("lisi02",22));  
  
  Iterator it = ts.iterator();
  
  while(it.hasNext()){
   System.out.println(it.next());
  }
}
}
class student{
private String name;
private int age;
student(String name, int age){
  this.name = name;
  this.age = age;
}
}

eclipse执行直接挂了,提示在  TreeSet ts = new TreeSet(); 这句时中断了。求指点。

作者: 不抛弃不放弃    时间: 2013-11-18 01:49
package test2;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSet_Test {
        public static void main(String []args){
                TreeSet ts= new TreeSet();
                 ts.add(new student("lisi02",22));  
                  
                   Iterator it = ts.iterator();
                  
                   while(it.hasNext()){
                    System.out.println(it.next());
                   }
        }
}

class student{
         private String name;
         private int age;
         student(String name, int age){
           this.name = name;
           this.age = age;
         }
         }

我运行了  没有问题啊  
作者: 松毛    时间: 2013-11-18 08:29
我的也没有挂啊!只不过打印出来的是student的hashCode。
作者: 陌上尘    时间: 2013-11-18 09:35
楼主你要导入TreeSet包,单导入util包是不行的
作者: 凝聚    时间: 2013-11-18 12:03
package answer;
import java.util.*;
public class TreeSett {
        public static void main(String[] args) {
                       
                TreeSet t=new TreeSet();
                t.add(new student("kisy",43));
                t.add(new student("eisu",43));
                Iterator it=t.iterator();
                while(it.hasNext())
                {
        //        System.out.println(        it.next());
                        student str=(student)it.next();
                        System.out.println(str.getName()+"......"+str.getAge())        ;
                }
                          }
                        }
                       
class student implements Comparable//该接口强制让学生具备比较性。
{
        private String name;
        private int age;
        student(String name,int age)
        {
                this.name=name;
                this.age=age;
        }
        public int compareTo(Object obj)
        {
               
                if(!(obj instanceof student))
                throw new RuntimeException("不是学生对象");
                student a=(student)obj;
                System.out.println(this.name+"...compareto..."+a.name);
                if(this.age>a.age)
                        return 1;
                if(this.age==a.age)
                {
                return        this.name.compareTo(a.name);
                }
               
                if(this.age==a.age)
                        return 0;

                return -1;
               
        }
        public String getName()
        {
                return name;
        }
        public int getAge()
        {
                return age;
        }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2