zwwcn

Just another WordPress.com site

Monthly Archives: June 2017

Endless loop with ListIterator .hasNext()

The following code will create an endless loop because java creates new iterator everytime you call list.iterator():

while(someList.iterator().hasNext()){
   someList.iterator().next().toString();
}

We should do it this way:

Iterator iterator = someList.iterator();
while(iterator.hasNext()){
   iterator.next().toString();
}
Advertisements