求好心人,帮初学者看看这个HashMap为什么打印不出来呢
package s;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HashMapTest {
public static void printElements(Collection c)
{
Iterator it=c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String[] args)
{
HashMap <Object,Object> hm=new HashMap <Object,Object> ();
hm.put( "one ", "zhangsan ");
hm.put( "two ", "lisi ");
hm.put( "three ", "wangwu ");
Set entry=hm.entrySet();
//printElements(entry);
Iterator it=entry.iterator();
while(it.hasNext());
{
Map.Entry me=(Map.Entry)it.next();
System.out.println(me.getKey()+ ": "+me.getValue());
}
}
}
------解决方案--------------------while(it.hasNext());
把这句后面的分号去掉,加上分号成死循环了。晕
------解决方案--------------------呵呵,楼上已经解答
------解决方案--------------------:)
------解决方案--------------------一个好的编程习惯是从小事养成的o
:)