ArrayList<String> list = new ArrayList<String>();
list.add("test");
while(list.listIterator().hasNext()) {
System.out.println(list.listIterator().next());
}
This generates an endless loop of lines with "test". Why does this happen and how to fix it?
The iterator created for hasNext is not the same that is for next.
ArrayList<String> list = new ArrayList<String>();
list.add("test");
Iterator listIterator = list.listIterator()
while(listIterator.hasNext()) {
System.out.println(listIterator.next());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With