Infinite loop while using iterator

前端 未结 4 1266
我在风中等你
我在风中等你 2021-01-29 11:45
Set key1 = map.keySet();
Iterator it1 = key1.iterator();
int cnt=0;
while (it1.hasNext()) {
  cnt++;
}

What are the chances that this code will result

4条回答
  •  春和景丽
    2021-01-29 12:09

    Actually it is resulting in infinite loop. My doubt is it is because I am not taking it1.next(); , is it true?

    Yes, this is true.

    However, you can find the count of a collection much easier:

    int cnt = map.size();
    

提交回复
热议问题