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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wxw_0818 中级黑马   /  2016-5-6 23:00  /  636 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

(1)定义一个学生类Student,包含属性:姓名(String name)、年龄(int age)
        (2)定义Map集合,用Student对象作为key,用字符串(此表示表示学生的住址)作为value
        (3)利用四种方式遍历Map集合中的内容,格式:key::value

2 个回复

倒序浏览
分享两种       
      Set<String>  set1=map.keySet();
        for(String key:set1)
        {
                Student s=map.get(key);
                System.out.println("家族:"+key+",姓名:"+s.getName()+",年龄:"+s.getAge());
        }
        System.out.println("--------------------");
        Set<Map.Entry<String,Student>> set2=map.entrySet();
        for(Map.Entry<String,Student> str:set2)
        {
                String name=str.getKey();
                Student stu=str.getValue();
               
                System.out.println("家族:"+name+",姓名:"+stu.getName()+",年龄:"+stu.getAge());
        }
回复 使用道具 举报
用fore遍历
,求key的set集合遍历
用迭代器遍历
用entryset()方法求出建值对象进行遍历
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马