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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 范龙波 高级黑马   /  2013-5-5 21:44  /  1447 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.*;
class Student implements Comparable<Student>
{
        private String name;
        private int age;
        public int hashCode()
        {
                return name.hashCode()+age*30;
        }
        public boolean equals(Object obj)
        {
                if (!(obj instanceof Student))
                {
                        throw new ClassCastException("传进的不是学生类型");
                }
                Student stu=(Student)obj;
                return this.name.equals(stu.name)&&this.age==stu.age;
                //return this.toString()==stu.toString();
        }
        public int compareTo(Student s)
        {
                int x=new Integer(this.age).compareTo(new Integer(s.age));
                if (x==0)
                {
                        return this.name.compareTo(s.name);
                }
                return x;
        }
       
        public void SetName(String name)
        {
                this.name=name;
        }
        public String GetName()
        {
                return name;
        }
        public void SetAge(int age)
        {
                this.age=age;
        }
        public int GetAge()
        {
                return age;
        }
        public String toString()
        {
                return "Student"+this.GetName()+"\t"+this.GetAge();
        }
        public Student(String name,int age)
        {
                this.name=name;
                this.age=age;
        }
}
class StudentDemo
{
        public static void main(String[] args)
        {
                HashMap<Student,String> hp=new HashMap<Student,String>();
                hp.put(new Student("张三",29),"北京");
                hp.put(new Student("李四",30),"上海");
                hp.put(new Student("王五",31),"深圳");
                Set<Student> s=hp.keySet();
                Iterator<Student> in=s.iterator();
                while (in.hasNext())
                {
                        Student stu=in.next();
                        String str=hp.get(stu);
                        System.out.println(stu+"\t"+str);
                }
                Set<Map.Entry<Student,String>> set=hp.entrySet();
                Iterator iter=set.iterator();
                while (iter.hasNext())
                {
                        Map.Entry<Student,String> map=iter.next();  //提示说iter.next();不兼容类型,没找到为什么希望能帮我找下错误谢谢。
                        Student xs=map.getKey();
                        String add=map.getValue();
                        System.out.println(xs+add);
               
                       

                }
        }
}

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

5 个回复

倒序浏览
本帖最后由 孟群峰 于 2013-5-5 22:01 编辑

问题出在你下面一句。
你没有给迭代器类型参数化,所以会报错。

Iterator iter=set.iterator();改成
Iterator<Map.Entry<Student,String>>iter = set.iterator()应该就没问题了,楼主你自己在试试吧
                while (iter.hasNext())
               
                        Map.Entry<Student,String> map=iter.next();  //提示说iter.next();不兼容类型,没找到为什么希望能帮我找下错误谢谢。
                        Student xs=map.getKey();
                        String add=map.getValue();
                        System.out.println(xs+add);
               

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
孟群峰 发表于 2013-5-5 22:00
问题出在你下面一句。
你没有给迭代器类型参数化,所以会报错。

嗯,加上之后错误提示没有了,可还是有点不明白,那个不是泛型吗?不加的话为什么会出现类型错误呢?是不是缺少了个转型的过程啊?有点不太理解。
回复 使用道具 举报
楼上正解!!!
回复 使用道具 举报
范龙波 发表于 2013-5-5 22:06
嗯,加上之后错误提示没有了,可还是有点不明白,那个不是泛型吗?不加的话为什么会出现类型错误呢?是不 ...

不加的话在编译器看来迭代器返回的是一个object类型的,所以就会报不兼容类型的错误,不用泛型的话就要用强制类型转换了。
回复 使用道具 举报
嗯,有点理解了,刚看到这一时有些联系不上,感觉前面的东西没弄扎实啊,谢谢了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马