-1

I have this piece of code in my class and it throws the java.util.ConcurrentModificationException on the bolded lines (indicated with** **)

public void solve() {
     **puzzleSolve(set.size(), sequence , set);**
}

//private helper method
protected void puzzleSolve(int k, String s, ArrayList<Character> u){


    ListIterator<Character> iter = u.listIterator();
    while(iter.hasNext())
    {
      Character c = iter.next();

      if(k==1){         //base case

            if(isAnswer(s+u.get(0)))

                System.out.println(s+u.get(0)+" is the correct sequence."+ '\n');
            return;
     }

     else{
         **iter.remove();**
         **puzzleSolve(k-1, s+c , u);** 
         iter.add(c);
         removeLastChar(s);


     }
    }

} //end of puzzleSolve method
halfer
  • 19,824
  • 17
  • 99
  • 186
LadyM
  • 5
  • 1

1 Answers1

4

Each of your recursive calls has its own iter. So you're modifying the list via multiple iterators at the same time which is not allowed. You'll have to redesign...

As to redesign - you could use for (int i = 0; i < u.size(); i++) and remove the ith element from the array before making the recursive call, then insert it back with add(i, element).

I am not saying mine is a good design, but it may work...

  • thank you. do you possible know how i should redesign the recursive calls ? I need this to happen: for every Character c in u, i want the algoritjm to run (multiple recursion) & for each c in u, it must first remove c from u & add it to S & then at the end make all sequences of members of U – LadyM Nov 17 '14 at 21:12