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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 费破的可 中级黑马   /  2013-11-1 15:34  /  641 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 费破的可 于 2013-11-1 18:18 编辑

import java.util.*;
class StuNameComparator implements Comparator<Student>
{
        public int compare(Student s1,Student s2)
        {
                int num = s1.getName().compareTo(s2.getName());
                if(num==0)
                        return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
                return num;
        }
}

class  MapTest2
{
        public static void main(String[] args)
        {
                TreeMap<Student,String> hm = new TreeMap<Student,String>(new StuNameComparator());
                hm.put(new Student("lishi01",24),"beijing");
                hm.put(new Student("lishi03",25),"wuahni");
                hm.put(new Student("lishi05",23),"shanghai");
                hm.put(new Student("lishi02",44),"nanjing");
                Set<Map.Entry<Student,String>> entrySet = hm.entrySet();
                Iterator<Map.Entry<Student,String>> it = entrySet.iterator();
                while(it.hasNext())
                {
                        Map.Entry<Student,String> me = it.next();
                        Student stu = me.getKey();
                        String adrr = me.getValue();
                        System.out.println(stu+"...."+adrr);
                }
        }
}
---------- javac ----------
MapTest2.java:11: 错误: 找不到符号
                int num = s1.getName().compareTo(s2.getName());
                                                   ^
  符号:   方法 getName()
  位置: 类型为Student的变量 s2
MapTest2.java:11: 错误: 找不到符号
                int num = s1.getName().compareTo(s2.getName());
                            ^
  符号:   方法 getName()
  位置: 类型为Student的变量 s1
MapTest2.java:13: 错误: 找不到符号
                        return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
                                                                                ^
  符号:   方法 getAge()
  位置: 类型为Student的变量 s2
MapTest2.java:13: 错误: 找不到符号
                        return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
                                             ^
  符号:   方法 getAge()
  位置: 类型为Student的变量 s1
4 个错误

输出完成 (耗时 1 秒) - 正常终止

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

4 个回复

倒序浏览
代码不全,不知你的Student对象从哪来的
回复 使用道具 举报
跑起,无误,是不是你没加载Student类啊
回复 使用道具 举报
零下五度的水 发表于 2013-11-1 17:37
跑起,无误,是不是你没加载Student类啊

加载了啊
回复 使用道具 举报
是你的Student类出错了 把Student类 发上来
你的Student类 明显就不对
你看看你的代码
int num = s1.getName().compareTo(s2.getName());
num是学号吗? 还是 什么之类的。  反正肯定是int吧。  但是 你=后面的 是一个String 吧。 ?  这样 肯定过不去啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马