出现异常了 请教高手!
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);
}
}
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);
}
}
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);
}
}
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);
}
}
//问题不是楼上几位说的那个,而是他没有实现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;
}
}