有什么建议可以避免在这个JAVA for循环中出现并发修改问题吗?

时间:2015-04-28 01:08:19

标签: java

在这个主要for循环的else语句中,我试图将每个新创建的堆栈添加到我的队列中,并且需要继续在同一个队列中继续,直到它为空或我找到结束字。非常感谢您的时间和精力。

    for (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());
            for ( String nextWord:temp2){
                @SuppressWarnings("unchecked")
                Stack<String> nextTempStack = (Stack<String>) next.clone();
                nextTempStack.push(nextWord);
                wordQ.add(nextTempStack);
            }
            buildLadder(start, next.peek().toString());
        }
    }

尝试使用Iterator。同样的问题。

Iterator<Stack<String>> myQIter = wordQ.iterator();
    while ( myQIter.hasNext()){
        Stack<String> tempStack = myQIter.next();
        //System.out.println("This is 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());
            for ( 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);这就是问题

2 个答案:

答案 0 :(得分:2)

所以你正在迭代一个列表并在你去的时候添加它。

尝试添加新的新列表,然后完成迭代,调用addAll添加到原始集合

答案 1 :(得分:0)

这是完整的课程。没有递归就可以工作。如果并发修改问题,int i循环的老派需要注意。

import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

/**
 * Class that created a word-ladder between two words (if possible)
 * @author GJ
 * @version 4/27/2015
 */
public class LadderBuilder {

    private Collection<String> dictionary;
    private Collection<String> usedWords = new HashSet<String>();

    public LadderBuilder(Collection<String> dictionary) {
        this.dictionary = dictionary;
    }


    public Deque<String> buildLadder(String start, String end){

        LinkedList<String> temp = new LinkedList<String>();
        Queue<Stack<String>> wordQ = new LinkedList<Stack<String>>();
        Deque<String> ladder = new LinkedList<String>();
        if ( start.length() != end.length()){
            System.err.println("Selected words are not of the same length.");
            System.exit(1);
        }
        temp = (LinkedList<String>) getWordsOneAway(end);
        Iterator<String> myIter = temp.iterator();
        while(myIter.hasNext()){

            Stack<String> words = new Stack<>();
            words.push(end);
            words.push(myIter.next());
            wordQ.add(words);
        }

        for (int i = 0; i < wordQ.size(); i++){
            Stack<String> temp3 = wordQ.poll();
            i--;
            if(temp3.peek().equals(start)){
                for(String each:temp3){
                    ladder.add(each);
                }
                return ladder;
            } 
            else {
                LinkedList<String> temp2 = new LinkedList<String>();
                temp2 = (LinkedList<String>) getWordsOneAway(temp3.peek());
                for ( String nextWord:temp2){
                    @SuppressWarnings("unchecked")
                    Stack<String> nextTempStack = (Stack<String>) temp3.clone();
                    nextTempStack.push(nextWord);
                    wordQ.add(nextTempStack);
                }
            }
        }
        return null;

    }

    private List<String> getWordsOneAway(String word){
        usedWords.add(word);
        List<String> oneAwayList = new LinkedList<String>();
        for (int i = 0; i < word.length(); i++) {
            char[] currCharArr = word.toCharArray();
            for (char c = 'a'; c <= 'z'; c++) {
                currCharArr[i] = c;
                String newWord = new String(currCharArr);
                if (dictionary.contains(newWord) && !(usedWords.contains(newWord))) {
                    oneAwayList.add(newWord);
                    usedWords.add(newWord);
                }
            }
        }

        return oneAwayList;
    }

}