Any suggestions to avoid concurrent modification issue in this JAVA for loop please? -


in else statement of main loop, trying add each newly created stacks queue , need keep going on over same queue until it's empty or find end word. thank lot time , effort.

    (stack<string> next:wordq){         if(next.peek().tostring().equals(start)){             for(string each:next){                 ladder.add(each);                 return ladder;             }         }          else {             linkedlist<string> temp2 = new linkedlist<string>();             temp2 = (linkedlist<string>) getwordsoneaway(next.peek().tostring());             ( string nextword:temp2){                 @suppresswarnings("unchecked")                 stack<string> nexttempstack = (stack<string>) next.clone();                 nexttempstack.push(nextword);                 wordq.add(nexttempstack);             }             buildladder(start, next.peek().tostring());         }     } 

tried using iterator. same issue.

iterator<stack<string>> myqiter = wordq.iterator();     while ( myqiter.hasnext()){         stack<string> tempstack = myqiter.next();         //system.out.println("this peek: " +tempstack.peek());         if(tempstack.peek().tostring().equals(start)){             for(string each:tempstack){                 ladder.add(each);                 return ladder;             }         }          else {             linkedlist<string> temp2 = new linkedlist<string>();             temp2 = (linkedlist<string>) getwordsoneaway(tempstack.peek().tostring());             ( string nextword:temp2){                 @suppresswarnings("unchecked")                 stack<string> nexttempstack = (stack<string>) tempstack.clone();                 nexttempstack.push(nextword);                 wordq.add(nexttempstack);             }             buildladder(start, tempstack.peek().tostring());         }     } 

wordq.add(nexttempstack); issue

so iterating through list , adding go.

try adding new new list, , have finished iterating through, call addall add original collection