黑马程序员技术交流社区

标题: 在写集合时出现的问题。。。 [打印本页]

作者: 一帆风顺    时间: 2012-12-25 14:08
标题: 在写集合时出现的问题。。。
本帖最后由 王博 于 2012-12-25 14:17 编辑

用myeclipse编写完代码编译能通过,也能运行,可是文件前面为什么会有个小叹号啊。。。加了其他文件又变成叉了。。。试了怎么改代码都没办法
下面附图:
[attach]11006[/attach]
下面附上我的代码
package com.itheima
import java.util.*;
class Test
{
    public static void main(String[] args)
    {
       //定义一个TreeSet集合
       TreeSet<Student> ts = new TreeSet<Student>();
       //添加元素
       ts.add(new Student("heima01",22,89));   
       ts.add(new Student("heima02",20,90));
       ts.add(new Student("heima03",19,56));
       ts.add(new Student("heima04",19,77));
       ts.add(new Student("heima05",20,65));

       //迭代器打印元素
       Iterator<Student> it = ts.iterator();   
       while(it.hasNext())
       {
           Student stu = (Student)it.next();
           System.out.println("姓名是"+stu.getName()+",年龄:"+stu.getAge()+",成绩:"+stu.getScore());
       }
    }
}
class Student implements Comparable    //错误应该是这里吧
{
    private String name;
    private int age;
    private double score;
    Student(String name,int age,double score)
    {
       this.name = name;
       this.age = age;
       this.score = score;
    }
    public int compareTo(Object obj)
    {
       if(!(obj instanceof Student))
           throw new RuntimeException("不是学生对象");
       Student s = (Student)obj;
       if(this.score<s.score)         
           return 1;
       if(this.score==s.score)
       {
           return this.name.compareTo(s.name);
       }
       return -1;

    }
    public String getName()
    {
       return name;
    }
    public int getAge()
    {
       return age;
    }
    public double getScore()
    {
       return score;
    }
}

360桌面截图6.jpg (72.23 KB, 下载次数: 23)

360桌面截图6.jpg

作者: 黄锦成    时间: 2012-12-25 14:34
没加泛型,Comparable<Student>
作者: 范德农    时间: 2012-12-25 14:42
亲,你的泛型呢。。。。。。。Comparable<Student>
作者: 一帆风顺    时间: 2012-12-25 15:59
是就在Comparable上加泛型么,我加过试了啊,怎么结果直接出现叉了?是我加的不对么??
作者: 陈泽鑫    时间: 2012-12-25 17:45
如果只是不想出现叹号的话,使用注释就好@SuppressWarnings("rawtypes")




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