0

Getting Concurrent Modification Exception while adding elements to ArrayList recursively.

import java.util.*;

public class Hello {

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        
        System.out.println(gss(str));
    }

    public static ArrayList<String> gss(String str) {
        if(str.length() == 0){
            ArrayList<String> list = new ArrayList<String>();
            list.add("");
            return list;
        }
        
        ArrayList<String> list = gss(str.substring(1));
        for(String temp : list){
            list.add(str.charAt(0)+temp);  // Problem  
        }
        
        return list;
    }

}
8
  • 1
    You need to avoid Concurrent Modification by adding list.addAll(list.stream().map(temp -> str.charAt(0)+temp).collect(Collectors.toList())); Commented Aug 17, 2020 at 8:31
  • Are you sure you want to add new elements to the list instead of modifying them? Commented Aug 17, 2020 at 8:32
  • Yes. @Amongalen Commented Aug 17, 2020 at 8:35
  • Can you provide an example of input and expected output? Commented Aug 17, 2020 at 8:38
  • 2
    This has nothing to do with recursion. You simply can't add to a list you are iterating. Commented Aug 17, 2020 at 9:05

1 Answer 1

0

Solution: To just form new ArrayList at each call stack and return it.

import java.io.*;

import java.util.*;

public class Main {

public static void main(String[] args) throws Exception {
    Scanner sc = new Scanner(System.in);
    String str = sc.next();
    
    System.out.println(gss(str));
}

public static ArrayList<String> gss(String str) {
    if(str.length() == 0){
        ArrayList<String> list = new ArrayList<String>();
        list.add("");
        return list;
    }
    
    ArrayList<String> list = gss(str.substring(1));
    ArrayList<String> listToReturn = new ArrayList<>();
    
    for(String temp : list){
        listToReturn.add(temp);
    }
    for(String temp : list){
        listToReturn.add(str.charAt(0) + temp);    
    }
    
    return listToReturn;
}

}

I have recently come across this blog.

Which says, It uses a transient variable called modCount, which keeps track of how many times a list is modified structurally. Structural modifications are those that change the size of the list, which may affect the progress of iteration and may yield incorrect results. Both Iterator and ListIterator uses this field to detect unexpected change. Other methods of List which structurally modify List also uses this method e.g. add(), remove().

Cause: The real cause of ConcurrentModficationException is inconsistent modCount. When you are iterating over ArrayList then Iterator's next() method keep track of modCount. If you modify the collection by adding or removing element then modCount will change and it will not match with the expected modCount, hence Iterator will throw ConcurrentModificationException.

Sign up to request clarification or add additional context in comments.

1 Comment

You can listToReturn.addAll(list)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.