黑马程序员技术交流社区
标题:
关于Map集合的扩展应用问题,高手指点 !!!!!
[打印本页]
作者:
尖卡斌引
时间:
2013-5-16 19:27
标题:
关于Map集合的扩展应用问题,高手指点 !!!!!
本帖最后由 尖卡斌引 于 2013-5-16 21:53 编辑
/*
演示示例:
传智播客有N多班级,
每个班级有N多学生。
利用Map集合建立 并打印所有学生的 id 和name。
*/
import java.util.*;
class Student
{
private String id;
private String name;
Student(String id,String name)
{
this.id = id;
this.name = name;
}
public String toString()
{
return id+"............"+name;
}
}
class MapDemo2
{
public static void main(String[] args)
{
HashMap<String,List<Student>> czbk = new HashMap<String,List<Student>>();
List<Student> yure = new ArrayList<Student>();
List<Student> jiuye = new ArrayList<Student>();
yure.add(new Student("y------001","zhangsan"));
yure.add(new Student("y------003","lisi"));
yure.add(new Student("y------004","wangwu"));
yure.add(new Student("y------002","xueliu"));
jiuye.add(new Student("j---=======---002","wangwu2"));
jiuye.add(new Student("j---=======---004","wangwu4"));
jiuye.add(new Student("j---=======---003","wangwu3"));
jiuye.add(new Student("j---=======---001","wangwu1"));
Iterator<String> it = czbk.keySet().iterator();
while(it.hasNext())
{
String roomName = it.next();
List<Student> room = czbk.get(roomName);
System.out.println(roomName);
getInfos(room);
}
}
public static void getInfos(List<Student> list)
{
Iterator<Student> it = list.iterator();
while(it.hasNext())
{
Student s = it.next();
System.out.println(s);
}
}
}
编译 运行都没有错误,就是没有结果。 高手指点
作者:
尖卡斌引
时间:
2013-5-16 19:28
没有错误提示的问题最头疼!!!!
作者:
飞鸟青崖
时间:
2013-5-16 19:37
你少两句代码。你并没有把两个List集合添加到HashMap集合中去。其余的没有问题。
HashMap<String,List<Student>> czbk = new HashMap<String,List<Student>>();
List<Student> yure = new ArrayList<Student>();
List<Student> jiuye = new ArrayList<Student>();
czbk.put("预热",yure);//把List集合添加进去。
czbk.put("就业",jiuye);
复制代码
作者:
殇_心。
时间:
2013-5-16 20:31
如果问题已解决,请及时修改分类,否则继续提问,谢谢合作!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2