黑马程序员技术交流社区

标题: Map练习里的问题帮忙解决下谢谢。 [打印本页]

作者: 范龙波    时间: 2013-5-5 21:44
标题: Map练习里的问题帮忙解决下谢谢。
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);
               
                       

                }
        }
}

作者: 孟群峰    时间: 2013-5-5 22:00
本帖最后由 孟群峰 于 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);
               
作者: 范龙波    时间: 2013-5-5 22:06
孟群峰 发表于 2013-5-5 22:00
问题出在你下面一句。
你没有给迭代器类型参数化,所以会报错。

嗯,加上之后错误提示没有了,可还是有点不明白,那个不是泛型吗?不加的话为什么会出现类型错误呢?是不是缺少了个转型的过程啊?有点不太理解。
作者: strawberry2013    时间: 2013-5-5 22:10
楼上正解!!!
作者: 孟群峰    时间: 2013-5-5 22:20
范龙波 发表于 2013-5-5 22:06
嗯,加上之后错误提示没有了,可还是有点不明白,那个不是泛型吗?不加的话为什么会出现类型错误呢?是不 ...

不加的话在编译器看来迭代器返回的是一个object类型的,所以就会报不兼容类型的错误,不用泛型的话就要用强制类型转换了。
作者: 范龙波    时间: 2013-5-5 22:23
嗯,有点理解了,刚看到这一时有些联系不上,感觉前面的东西没弄扎实啊,谢谢了。




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