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?
hasNext()
always returnstrue
. – Desultoryfor (ListIterator<String> it = list.listIterator(); it.hasNext();) { /* looped code */ }
, limits the scope of the iterator to just that loop. – Ashla