Reputation: 67
The purpose of my code is to have the user enter a car name, then search through the array list and find an object that matches what the user entered. Whenever I run the code, I get the java.util.ConcurrentModificationException error. An explanation of what this error means and advice for fixing it is much appreciated :)
public static void arrayList()
{
//Declarations
ArrayList<String> list = new ArrayList<String> ();
ListIterator<String> iterator = list.listIterator();
list.add ("Aston Martin");
list.add ("Ferrari");
Scanner scan = new Scanner(System.in);
String car = new String();
String search = new String();
//Prompts user to enter car name
System.out.println ("Enter car name: ");
car = scan.nextLine();
//Searches array list for car
while (iterator.hasNext())
{
search = iterator.next();
if (search.equalsIgnoreCase (car))
{
System.out.println (search);
}
}
}
Upvotes: 1
Views: 2766
Reputation: 11
I met the same problem, but a little bit different on what involved in ConcurrentModificationException
. Mine, actually was a sort
method call on a collection while initializing. I had an iteration on data and in another thread a sort method processing. Somehow, the Collection.sort(...)
process triggers this Exception too.
Initial code on iteration was:
...
//_Methode_a
for (final Entry<String, List<GmScope>> scopes : model.getProjectData().entrySet()) {
for (final GmScope scope : scopes.getValue()) { //Where exception occures
...
//Method_b using a different Thread
...
Collections.sort(scopeList, new Comparator<GmScope>() {
@Override
public int compare(final GmScope o1, final GmScope o2) {
return o1.getScopeName().compareTo(o2.getScopeName());
}
});
...
To fix the problem I have implement Comparable
interface to my bean and define the comparision process there. Then I have removed the Collection.sort(...)
defined in my method.
public class GmScope implements Comparable<GmScope> {
...
@Override
public int compareTo(final GmScope o1) {
return this.getScopeName().compareTo(o1.getScopeName());
}
}
Upvotes: 0
Reputation: 338
You open an iterator and then change the structure of the list, use a for each loop instead of iterator and if you still want to use ListIterator then open the iterator after adding the elements to the list like this..
ArrayList<String> list = new ArrayList<String>();
list.add("Aston Martin");
list.add("Ferrari");
ListIterator<String> iterator = list.listIterator();
Upvotes: 1
Reputation: 77177
The Javadocs for the relevant collections and ConcurrentModificationException
are clear:
This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.
You started an iteration over list
but then modified it and came back to the iterator.
Don't open your iterator until right before you're about to use it. Even better, since you don't need access to remove()
, just use an enhanced for
loop:
for(String item: list) {
if(item.equalsIgnoreCase (car)) {
System.out.println(item);
}
}
Upvotes: 3