出现异常了 请教高手!
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);
}
}
解决方案 »
- 急..编程作业..(英文)
- 很简单的一个小问题!^_^ 关于StringBuffer 的
- 请问怎样把DOM树输出的XML内容显示在普通的文本编辑器里,使其带有格式?
- clip rectangle应该怎么翻译?在看一篇关于AWT和Swing绘图的文章
- 我这有两段程序是一端向另一端发一段话,然后在另外一段打印出来这句话,ip地址和端口。我指定地端口号是3000,可是打印出来端口号却不是
- 关于java.sql.PreparedStatement.executeUpdate 返回受影响的行
- 8月份中国的SCJP考试会不会升级到1.4?
- linkguic(rake)兄还在吗?再请教一下!
- 斑竹,各位大虾,问题又来了!
- 又没有人推荐一下 初学javase应该看点什么书
- 懂JDOM和XML的进来看一下
- 请推荐本比较好的参考书
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;
}
}