Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';我想不出是什么';“s触发”;java.util.ConcurrentModificationException“;_Java_Exception - Fatal编程技术网

Can';我想不出是什么';“s触发”;java.util.ConcurrentModificationException“;

Can';我想不出是什么';“s触发”;java.util.ConcurrentModificationException“;,java,exception,Java,Exception,我的代码抛出了一个我以前从未见过的错误。嘿!我想我正在学习;)无论如何,我读了一些书,通常在迭代过程中修改正在迭代的列表时会抛出这个错误。然而,我很确定我没有修改它。当在partition()上抛出错误时,如果我没有在updateCurrentList()中为currentList赋值(通过注释代码),程序将不再抛出错误。在my play()方法中,这两个函数一个接一个地被调用,但是列表迭代应该在进行更改时完成。我错过了什么?我必须关闭迭代器吗 package hangman; import j

我的代码抛出了一个我以前从未见过的错误。嘿!我想我正在学习;)无论如何,我读了一些书,通常在迭代过程中修改正在迭代的列表时会抛出这个错误。然而,我很确定我没有修改它。当在partition()上抛出错误时,如果我没有在updateCurrentList()中为currentList赋值(通过注释代码),程序将不再抛出错误。在my play()方法中,这两个函数一个接一个地被调用,但是列表迭代应该在进行更改时完成。我错过了什么?我必须关闭迭代器吗

package hangman;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;

public class Hangman {
    private Map<String, List<String>> wordPartitions; // groups words according to positions of guessed letter
    private List<String> currentList; // remaining possible words that fit the information given so far
    Set<Character> wrongGuesses; // holds all the "wrong" guesses so far
    StringBuilder guessString; // current state of the word being guessed
    String justHyphens; // for checking whether a guess was "wrong"

    // initialize fields
    // currentList should contain all (and only) words of length wordLength

// justHyphens and guessString should consist of wordLength hyphens
public Hangman(int wordLength) throws FileNotFoundException {
    this.currentList = new ArrayList<String>();
    addWords(wordLength);
    wrongGuesses = new HashSet();

    for(int i = 0; i < wordLength; i++) {
        justHyphens += "-";
    }
    guessString = new StringBuilder();
    wordPartitions = new HashMap();
}

private void addWords(int wordLength) throws FileNotFoundException {
    Scanner words = new Scanner(new File("lexicon.txt"));
    String word = "";

    while(words.hasNext()) {
        word = words.next();
        if (word.length() == wordLength) {
            currentList.add(word);
        }
    }
}

// main loop
public void play() {
    char choice;

    do {
        choice = getUserChoice();
        partition(choice);
        updateCurrentList(choice);
    } while (!gameOver());
        endMessage();
}

// display the guessString and the missed guesses
// and get the next guess
private char getUserChoice() {
   //generate a string from the incorrect choices char list
    String wrong = "";
    char letter;

    if(!wrongGuesses.isEmpty()) {
        Iterator<Character> letters = wrongGuesses.iterator();
        letter = letters.next(); 

        while(letters.hasNext()) {
            letter = letters.next();
            wrong += ", " + letter;
        }
    }

    String letterStr = JOptionPane.showInputDialog("Incorrect choices: "+ wrong +"\n Tested letters: "+ guessString.toString() +"\nplease input a letter.");
    return letterStr.charAt(0);
}


// use wordPartitions to partition currentList using
// keys returned by getPartitionKey()
private void partition(char choice) {

    String word = "";
    String key = "";
    List<String> tempList = new ArrayList<String>();

    Iterator<String> words = currentList.iterator();
    //Generate a key for each word and add to appropriate arraylist within map.
    while(words.hasNext()) {
        word = words.next();
        key = getPartitionKey(word, choice);

        if(wordPartitions.containsKey(key)) {
            tempList = wordPartitions.get(key);
            tempList.add(word);
            wordPartitions.put(key, tempList);
        } else {
            tempList.clear();
            tempList.add(word);
            wordPartitions.put(key, new ArrayList<String>());                
        }            
    }
}

// update currentList to be a copy of the longest partition
// if choice was "wrong", add choice to wrongGuesses
// if choice was "right", update guessString
private void updateCurrentList(char choice) {        
    String key = findLongestList();

    currentList = wordPartitions.get(key);

    if(key.equals(justHyphens)) {
        wrongGuesses.add(choice);
    } else {
        addLetterToGuessString(guessString, choice, key);
    }
}

private String findLongestList() {
    Set<String> keySet = wordPartitions.keySet();
    Iterator<String> keys = keySet.iterator();
    String maxKey = "";
    int maxKeyLength = 0;
    List<String> tempList;
    String tempKey = "";

    while(keys.hasNext()) {
        tempKey = keys.next();
        tempList = wordPartitions.get(tempKey);

        if(tempList.size() > maxKeyLength) {
            maxKeyLength = tempList.size();
            maxKey = tempKey;
        }
    }

    return maxKey;
}

// checks for end of game
private boolean gameOver() {
    return false;
}

// display the guessString and the missed guesses
// and print "Congratulations!"
private void endMessage() {
    JOptionPane.showMessageDialog(null, "Congrats, yo!");
}

// returns string with '-' in place of each
// letter that is NOT the guessed letter
private String getPartitionKey(String s, char c) {
    String word = "";
    String letter = Character.toString(c);
    for(int i = 0; i < s.length(); i++) {
        if(s.charAt(i) == c) {
            word += letter;
        } else {
            word += "-";
        }
    }

    return word;
}
// update guessString with the guessed letter
private void addLetterToGuessString(StringBuilder guessString, char letter, String key) {
    for(int i = 0; i < key.length(); i++) {
        if(key.charAt(i) != '-') {
            guessString.setCharAt(i, key.charAt(i)); 
        } 
    }
}
packagehangman;
导入java.io.*;
导入java.util.*;
导入javax.swing.JOptionPane;
公共级刽子手{
私有映射wordPartitions;//根据猜测字母的位置对单词进行分组
private List currentList;//符合到目前为止给出的信息的剩余可能单词
设置错误猜测;//保留到目前为止所有的“错误”猜测
StringBuilder guessString;//正在猜测的单词的当前状态
String justHyphens;//用于检查猜测是否“错误”
//初始化字段
//currentList应包含所有(且仅限于)字长为wordLength的单词
//justHyphens和guessString应该由字长连字符组成
公共Hangman(int字长)抛出FileNotFoundException{
this.currentList=新的ArrayList();
addWords(字长);
错误猜测=新HashSet();
for(int i=0;imaxKeyLength){
maxKeyLength=templast.size();
maxKey=tempKey;
}
}
返回maxKey;
}
//检查游戏是否结束
私有布尔gameOver(){
返回false;
}
//显示猜测字符串和错过的猜测
//然后打印“恭喜你!”
私有void endMessage(){
showMessageDialog(null,“恭喜,哟!”);
}
//返回字符串,并用“-”代替每个
//不是猜测的字母
私有字符串getPartitionKey(字符串s,字符c){
字串=”;
字符串字母=字符.toString(c);
对于(int i=0;i

}

问题在于,在对集合进行迭代时,您正在修改该集合

集合是
currentList
,您正在
partition()
中对其进行迭代。将单词添加到
templast
此处时,可以对其进行修改:

key = getPartitionKey(word, choice);

if(wordPartitions.containsKey(key)) {
    tempList = wordPartitions.get(key);
    tempList.add(word);
    wordPartitions.put(key, tempList);
} else {
为什么??因为之前您从
play()
调用了
updateCurrentList()

您更新了
currentList

String key = findLongestList();

currentList = wordPartitions.get(key);
因此,如果<
String key = findLongestList();

currentList = wordPartitions.get(key);
if(wordPartitions.containsKey(key)) {
    tempList = wordPartitions.get(key);
} else {
    wordPartitions.put(key, new ArrayList<String>());                
}

if (tempList!=currentList) {
    tempList.add(word);
}