黑马程序员技术交流社区
标题:
Map扩展练习问题
[打印本页]
作者:
孙飞
时间:
2012-7-9 12:00
标题:
Map扩展练习问题
本帖最后由 feigecal 于 2012-7-9 12:54 编辑
import java.util.*;
class Test3
{
public static void main(String[] args)
{
demo();
}
public static void getInfos(List<Student> list)
{
Iterator<Student> it = list.iterator();
while(it.hasNext())
{
Student s = it.next();
System.out.println(s);
}
}
public static void demo()
{
HashMap<String,List<Student>> hm=new HashMap<String,List<Student>>();
List<Student> yure=new ArrayList<Student>();
List<Student> jiuye=new ArrayList<Student>();
hm.put("yureban",yure);
hm.put("jiuyeban",jiuye);
yure.add(new Student("ok1","aa"));
yure.add(new Student("ok3","aas"));
jiuye.add(new Student("ok1","aa"));
jiuye.add(new Student("ok3","aas"));
Set<String> key=hm.keySet();
Iterator<String> it=key.iterator();
while(it.hasNext())
{
String s=it.next();
List<Student> stu=hm.get(s);
getInfos(stu);
}
/*Set<Map.Entry<String,List<Student>>> entry=hm.entrySet();
Iterator<Map.Entry<String,List<Student>>> it=entry.iterator();
while(it.hasNext())
{
Map.Entry<String,List<Student>> s=it.next();
String s1=s.getKey();
List<Student> s2=s.getValue();
Iterator<Student> it2=s2.iterator();
while(it2.hasNext())
{
Student stu=it2.next();
System.out.println(stu.toString());
}
}*/
}
}
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;
}
}
上题蓝色部分是被注释掉的,用红色部分时是编译通过,打印抛异常,哪里错了?还有,如果把红色部分注释了,用蓝色部分(用entryKey方法)也是同样问题,为什么出错?
作者:
高原
时间:
2012-7-9 12:13
哥们你在逗我……我把你的代码复制过去,一个字没改,编译运行完全正常……
作者:
游兴钟
时间:
2012-7-9 12:23
本帖最后由 achilles 于 2012-7-9 12:31 编辑
import java.util.*;
class Test3
{
public static void main(String[] args)
{
demo();
}
public static void getInfos(List<Student> list)
{
Iterator<Student> it = list.iterator();
while(it.hasNext())
{
Student s = it.next();
System.out.println(s);
}
}
public static void demo()
{
HashMap<String,List<Student>> hm=new HashMap<String,List<Student>>();
List<Student> yure=new ArrayList<Student>();
List<Student> jiuye=new ArrayList<Student>();
hm.put("yureban",yure);
hm.put("jiuyeban",jiuye);
yure.add(new Student("ok1","aa"));
yure.add(new Student("ok3","aas"));
jiuye.add(new Student("ok1","aa"));
jiuye.add(new Student("ok3","aas"));
/* Set<String> key=hm.keySet();
Iterator<String> it=key.iterator();
while(it.hasNext())
{
String s=it.next();
List<Student> stu=hm.get(s);
getInfos(stu);
}*/
Set<Map.Entry<String,List<Student>>> entry=hm.entrySet();
Iterator<Map.Entry<String,List<Student>>> it=entry.iterator();
while(it.hasNext())
{
Map.Entry<String,List<Student>> s=it.next();
String s1=s.getKey();
List<Student> s2=s.getValue();
getInfos(s2);
//
楼主既然已经封装了取student的方法 直接调用好了
/*Iterator<Student> it2=s2.iterator();
while(it2.hasNext())
{
Student stu=it2.next();
System.out.println(stu.toString());
}*/
}
}
}
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;
}
}
红色部分运行过 没有问题啊
作者:
孙飞
时间:
2012-7-9 12:53
高原 发表于 2012-7-9 12:13
哥们你在逗我……我把你的代码复制过去,一个字没改,编译运行完全正常…… ...
现在运行又正常了,问之前一直抛异常,我说呢找不到错误,呵呵
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2