How do I resolve this error when I run my program? I can pinpoint that it has to do with adding a string to the ArrayList in the doubleValues(ArrayList<String list>) method. When I use the loop to print out the list it runs just fine.
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at com.codegym.task.task07.task0717.Solution.doubleValues(Solution.java:33)
at com.codegym.task.task07.task0717.Solution.main(Solution.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
package com.codegym.task.task07.task0717;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Duplicating words
*/
public class Solution {
public static void main(String[] args) throws Exception {
// Read strings from the console and declare an ArrayList here
ArrayList<String> list = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < 10; i++) {
list.add(reader.readLine());
}
ArrayList<String> result = doubleValues(list);
// Display result
/*System.out.println("\nOutput");
for (String str : result) {
System.out.println(str);
System.out.println(str);
}*/
}
public static ArrayList<String> doubleValues(ArrayList<String> list) {
//write your code here
for (String str : list) {
list.add(str);
/*list.add(str);
list.add(str);*/
/*System.out.println(str);
System.out.println(str);*/
}
return list;
}
}