为什么使用方法一可正常迭代打印,使用方法二就会漏掉几条呢??
//取出元素,按键查找
Set<Map.Entry<Student, String>> entrieSet = map.entrySet();
Iterator<Map.Entry<Student, String>> it = entrieSet.iterator();
while (it.hasNext()){
//方法一
/* Map.Entry<Student, String> next = it.next();
Student key = next.getKey();
String value = next.getValue();*/
//方法二
Student key = it.next().getKey();
String value = it.next().getValue();
System.out.println(key+"=="+value);
}
正常打印输出(方法一):
方法二输出:
求求了,孩子想不明白
Student key = it.next().getKey();
String value = it.next().getValue();
这两句代码,读了两次内容导致的。
因为每次调用next就跳过一个,你每次循环调用了两次。
一个next一次下一步