Ekansh Rastogi
Ekansh Rastogi

Reputation: 2546

Concurrent ModificationException

I have a piece of code and i am not sure why i am getting the concurrentModificationException

import java.util.*;
import java.util.LinkedList;
import java.util.Scanner;
public class CreateList{
    List<Integer> nestedList;
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Nested List");
        CreateList obj = new CreateList();
        System.out.println(obj.createNestedList(sc.next()));
    }
    public List<Integer> createNestedList(String str){
        List<Object> aux = new LinkedList<Object>();
        nestedList = new LinkedList<Integer>();
        for(int itr=0; itr<str.length();itr++){
            char c = str.charAt(itr);
            if(c != ']' && c != ',')
            aux.add(c);
            else if(c == ']'){
                Object o = aux.subList(aux.lastIndexOf('[')+1,aux.size());
                aux = aux.subList(0,aux.lastIndexOf('['));
                aux.add(o);// THIS LINE IS THROWING THE EXCEPTION
                System.out.println(o);
                System.out.println(aux);
            }
        }
        System.out.println(aux);
        return nestedList;
    }
}

There is no thread using this code other then the main thread. Any idea what am i missing

Upvotes: 1

Views: 50

Answers (3)

dkatzel
dkatzel

Reputation: 31658

I think the problem is the use of subLists. Specifically

aux = aux.subList(0,aux.lastIndexOf('['));

And then later

aux.add(o);// THIS LINE IS THROWING THE EXCEPTION

This changes the size of the sublist vs the size of the original list.

The javadoc of sublist says:

The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

The sublist instance returned by .subList() throws an exception if the modcounts of the sublist vs the original list differ.

The solution is to make a new list using the elements from the sublist

aux = new LinkedList<>(aux.subList(0,aux.lastIndexOf('[')));

Upvotes: 3

hfontanez
hfontanez

Reputation: 6188

You must use a list iterator if you intend to modify the list while iterating.

List<Object> aux = new LinkedList<Object>();
...
ListIterator<Object> listIterator = aux.listIterator();

// replace the for loop with while. just to make it easier, but can be done with for loop
while(listIterator.hasNext()) {
    ...
    listIterator.add(o);
    ...
}

If you replace your code with this, it should work.

Upvotes: 1

MeetTitan
MeetTitan

Reputation: 3568

You need to create another list instead of getting a sublist and adding a sublist to it.

try something like this instead:

List<T> list = new LinkedList<T>(someList.subList(0,n));

Upvotes: 1

Related Questions