本帖最后由 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方法)也是同样问题,为什么出错? |
|