出现异常了 请教高手!
import java.util.*;
public class tree {
    int s ;
    tree (int j){
        s=j;
    }
    public static void main(String[] args) {
       TreeMap t=new TreeMap();
       for (int i=0;i<5;i++)
           t.put(new tree(i),new Integer(i+2));
       System.out.print(t);
    }
}

解决方案 »

  1.   

    import java.util.*; 
    public class tree { 
        int s ; 
        tree (int j){ 
            s=j; 
        } 
        public static void main(String[] args) { 
           TreeMap t=new TreeMap(); 
           for (int i=0;i <5;i++) 
               t.put(new tree(i).toString(),new Integer(i+2));        System.out.print(t); 
        } 
    }
      

  2.   

    import java.util.*; 
    public class tree { 
        int s ; 
        tree (int j){ 
            s=j; 
        } 
        public static void main(String[] args) { 
           TreeMap t=new TreeMap(); 
           for (int i=0;i <5;i++) 
               t.put(new tree(i).toString(),(i+2));
           System.out.print(t); 
        } 
    }
      

  3.   

    不过 toString();能不能代替用compare接口实现的一切?
      

  4.   


    //问题不是楼上几位说的那个,而是他没有实现Comparable,因为TreeMap,的put方法,插入的时候会调用其compareTo方法,也就是这个集合是有序集合,就是内部自然排序!所以必须要实现Comparable接口
    package B;import java.util.TreeMap;public class tree implements Comparable<tree> {    int s;    tree(int j) {
            s = j;
        }    public static void main(String[] args) {
            TreeMap t = new TreeMap();
            for (int i = 0; i < 5; i++)
                t.put(
                    new tree(i),
                    new Integer(i + 2));
            System.out.println(t);
        }    public int compareTo(tree o) {
            if (o == null)
                return -1;
            if (this.s > o.s)
                return 1;
            if (this.s == o.s)
                return 0;
            if (this.s < o.s)
                return -1;
            return -1;
        }
    }
      

  5.   

    compareTo方法内部的你可以随便设计!按照你的需求~就是比较tree的两个对象的方法!