你迭代器要是用泛型。
Iterator
还有就是在使用迭代器的时候,你判断一次,游标走了2次,使用了2次next();
while(it.hasNext()){
int key=it.next();
if(key==1)
.........//后边自己写了
}
你的目的是遍历HashMap对吧,其实遍历Map系列并不一定要用迭代器,个人喜欢用另一种方式,简单好用(不过这仅仅是个人看法)。代码如下:
HashMap
map.put(1, "吕布");
map.put(2, "赵云");
map.put(3,"典韦");
for(Map.Entry
System.out.println(entry.getKey()+":"+entry.getValue());
}
next会前移一位,集合里只有一项,你调用两次next