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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张先龙 中级黑马   /  2013-4-7 10:51  /  1201 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张先龙 于 2013-4-7 19:21 编辑

代码如下:

import java.util.*;

class Student implements Comparable<Student>
{
        private String name;

        private int  age;

        Student(String name, int age)
        {
                this.name=name;
                this.age=age;
        }
    public int hashCode()
        {
        return name.hashCode()+age*35;
        }

        public boolean equals (Object obj)
        {
                if (!(obj instanceof Student))
                        
                   throw new ClassCastException("类型不匹配");

                Student s=(Student)obj;

                return this.name.equals(s.name)&&this.age==s.age;
        }

        public int compareTo(Student s)
        {
        int num= new Integer(this.age).compareTo(new Integer(s.age));

                if (num==0)
               
                return this.name.compareTo(s.name);

                return num;
        
        }

        public String getName()
        {
                return name;
        }

        public int getAge()
        {
                return age;
        }

        public String toString()
        {
                return name+"..."+age;
        }

}
class  MapEntryDemo
{
        public static void main(String[] args)
        {
                HashMap<Student,String> stu= new HashMap<Student,String>();

                stu.put(new Student("小遥",8),"三年级");
                stu.put(new Student("小豪",6),"一年级");
                stu.put(new Student("小程",2),"家里蹲");
                stu.put(new Student("小明",9),"六年级");
                stu.put(new Student("小露",12),"七年级");

                Set<Student> keyset=stu.keySet();

                Iterator<Student> it =stu.iterator();

                while(it.hasNext())
                {
                        Student key=it.next();

                        String  grate=stu.get(key);
                        
                        System.out.println(grate+key);
                }

                Set<Map.Entry<Student,String>> entryset=stu.entrySet();

                Iterator<Map.Entry<Student,String>> iter=entryset.iterator();

                while(iter.hasNext())
                {
                        Map.Entry<Student,String> me=iter.next();
                        System.out.println(me.getKey()+ me.getValue());
                }

        }
}
编译不通过啊。。

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

5 个回复

倒序浏览
把编译错误的信息贴一下 这样看起来费劲
回复 使用道具 举报
编译不通过是因为有语法错误,将stu.iterator()改成new ArrayList().iterator();就行了
import java.util.*;

class Student implements Comparable<Student>
{
        private String name;

        private int  age;

        Student(String name, int age)
        {
                this.name=name;
                this.age=age;
        }
    public int hashCode()
        {
        return name.hashCode()+age*35;
        }

        public boolean equals (Object obj)
        {
                if (!(obj instanceof Student))
                        
                   throw new ClassCastException("类型不匹配");

                Student s=(Student)obj;

                return this.name.equals(s.name)&&this.age==s.age;
        }

        public int compareTo(Student s)
        {
        int num= new Integer(this.age).compareTo(new Integer(s.age));

                if (num==0)
               
                return this.name.compareTo(s.name);

                return num;
        
        }

        public String getName()
        {
                return name;
        }

        public int getAge()
        {
                return age;
        }

        public String toString()
        {
                return name+"..."+age;
        }

}
class  MapEntryDemo

{
        public static void main(String[] args)
        {
                HashMap<Student,String> stu= new HashMap<Student,String>();

                stu.put(new Student("小遥",8),"三年级");
                stu.put(new Student("小豪",6),"一年级");
                stu.put(new Student("小程",2),"家里蹲");
                stu.put(new Student("小明",9),"六年级");
                stu.put(new Student("小露",12),"七年级");

                Set<Student> keyset=stu.keySet();

                Iterator<Student> it = new ArrayList().iterator();
;
            
               
                while(it.hasNext())
                {
                        Student key=it.next();

                        String  grate=stu.get(key);
                        
                        System.out.println(grate+key);
                }

                Set<Map.Entry<Student,String>> entryset=stu.entrySet();

                Iterator<Map.Entry<Student,String>> iter=entryset.iterator();

                while(iter.hasNext())
                {
                        Map.Entry<Student,String> me=iter.next();
                        System.out.println(me.getKey()+ me.getValue());
                }

        }
}

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
  Set<Student> keyset=stu.keySet();

Iterator<Student> it =stu.iterator();//这个stu应该改成keyset,stu是map集合,不能直接用迭代器遍历的。

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
若还有问题,继续追问; 没有的话,将帖子分类改成【已解决】~
回复 使用道具 举报
解决了 ,,3Q各位!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马