A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© djx900 黑马帝   /  2012-3-7 16:55  /  126095 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package cn.itcast.eclips;

  2. import java.sql.Statement;
  3. import java.util.*;


  4. public class demo1 {

  5.         /**
  6.          * @param args
  7.          */
  8.         public static void main (String[] args) {
  9.                 TreeSet ts = new TreeSet();
  10.                
  11.                 ts.add(new Student("li",22,33));
  12.                 ts.add(new Student("zhang",21,99));
  13.                 ts.add(new Student("wang",23,20));
  14.                 ts.add(new Student("dong",25,50));
  15.                 ts.add(new Student("ka",37,60));
  16.                
  17.                 Iterator it = ts.iterator();
  18.                 while( ((Iterator) it).hasNext())
  19.                 {
  20.                         Student stu = (Student)it.next();
  21.                         System.out.print(stu.getGrade()+"..."+stu.getAge()+"..."+stu.getGrade() );
  22.                 }
  23.    
  24.         }


  25. class Student  implements Comparable

  26. private String name;   
  27. private int age;   
  28. private int grade;   

  29. public Student(String name,int age,int grade)
  30. {   
  31. this.name = name;   
  32. this.age = age;   
  33. this.grade = grade;   
  34. }   
  35. public String getName() {   
  36. return name;   
  37. }   

  38. public void setName(String name) {   
  39. this.name = name;   

  40. }   
  41. public int getAge() {   

  42. return age;   

  43. }   
  44. public void setAge(int age)
  45. {   
  46. this.age = age;   
  47. }   
  48. public int getGrade()
  49. {   
  50. return grade;   

  51. }   
  52. public void setGrade(int grade)
  53. {   
  54. this.grade = grade;   
  55. }   


  56. public int compareto(Object obj)
  57. {
  58.         Student si = (Student)obj;
  59. if(!(obj instanceof Student))
  60.            throw new RuntimeException("不是学生");
  61.          Student s = (Student)obj;
  62.     if(this.grade>s.grade)
  63.             return 1;
  64.     if(this.grade<s.grade)
  65.        return -1;
  66.     if(this.grade==s.grade)
  67.             return this.name.compareTo(s.name);
  68. }



  69. }
复制代码

9 个回复

倒序浏览
Exception in thread "main" java.lang.Error: Unresolved compilation problem:

        at cn.itcast.eclips.demo1.main(demo1.java:12)
总是显示这个错误 高手指教下啊
回复 使用道具 举报
你运行的不是这个主函数吧? {:soso_e141:}
回复 使用道具 举报
编译有错误~~~编译么通过,有不合语法的错~~~
回复 使用道具 举报
代码这么写的  没别的文件都
回复 使用道具 举报
求 语法 错误
回复 使用道具 举报
public int compareto(Object obj)

这个方法名错了应该是compareTo
回复 使用道具 举报
本帖最后由 许圣建 于 2012-3-7 21:49 编辑

.Error: Unresolved compilation problem原因是compareTo()没覆写到,你是to小写了,楼主大小写很不规范,dome1首字母也没大写
另外我想问问,第22行代码为什么非要写(Iterator it)it.next()?很奇怪,难道迭代器内部实现代码有问题,不能保证next()方法返回一个迭代器?
提醒一下,大部分异常都是可以通过名称来分辨的,名称那么长都是有意义的。
回复 使用道具 举报
校长 中级黑马 2015-10-18 23:47:57
9#
是否最末尾多了个“  }  ” ?
回复 使用道具 举报
看来规范很重要啊,我也遇到这个问题了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马