So im currently testing ArrayList and with the code below, im always getting a java.util.ConcurrentModificationException
at line 23. I read the documentation and it states
it is not generally permissible for one thread to modify a Collection while another thread is iterating over it
but im not modifying the collection while iterating over it, im doing it beforehand, so im not sure what im doing wrong here.
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> a = new ArrayList<String>();
ListIterator<String> lt = a.listIterator();
System.out.print("Enter a word: ");
String s = scanner.nextLine();
while (!s.equals("")) {
a.add(s);
System.out.print("Enter another word: ");
s = scanner.nextLine();
}
while (lt.hasNext()) {
String z = lt.next(); // line 23
System.out.println(z);
}
}
}