我有2个列表List<String> existingGuesses
,其中包含字符,List<String> word = new ArrayList<String>(words)
包含的字词是words
列表的副本,如下面的代码所示。我要做的是迭代List word
的所有单词并遍历list existingGuesses
的字符。然后我想将List existingGuesses
的第一个字符与List word
的所有字词逐个进行比较,如果找到匹配项,那么我想要remove
列表中的单词。然后对下一个字符进行相同操作并继续,直到不再有words
进行比较。到现在为止,我只能声明一个iterator
,没有必要使用iterator
,但我不知道其他任何方式。我的代码如下:
public List<String> getWordOptions(List<String> existingGuesses, String newGuess)
{
List<String> word = new ArrayList<String>(words);
String c = existingGuesses.get(0); //trying to get the first character
ListIterator<String> iterator = word.listIterator(); //iterator to iterate thorugh word list
while(iterator.hasNext())
{
if(word.contains(c)) //trying to compare
{
word.remove(c);
}
}
return null;
}
有人可以帮帮我吗?原始List words
是:
private List<String> words = new ArrayList<String>() {
{
// DO NOT CHANGE THESE WORDS!
String w =
"sentence\n"
+ "together\n"
+ "children\n"
+ "mountain\n"
+ "chipmunk\n"
+ "crashing\n"
+ "drinking\n"
+ "insisted\n"
+ "insulted\n"
+ "invented\n"
+ "squinted\n"
+ "standing\n"
+ "swishing\n"
+ "talented\n"
+ "whiplash\n"
+ "complain\n"
+ "granddad\n"
+ "sprinkle\n"
+ "surprise\n"
+ "umbrella\n"
+ "anything\n"
+ "anywhere\n"
+ "baseball\n"
+ "birthday\n"
+ "bluebird\n"
+ "cheerful\n"
+ "colorful\n"
+ "daylight\n"
+ "doghouse\n"
+ "driveway\n"
+ "everyone\n"
+ "faithful\n"
+ "flagpole\n"
+ "graceful\n"
+ "grateful\n"
+ "homemade\n"
+ "homework\n"
+ "housefly\n"
+ "kickball\n"
+ "kingfish\n"
+ "knockout\n"
+ "knothole\n"
+ "lipstick\n"
+ "lunchbox\n"
+ "newscast\n"
+ "nickname\n"
+ "peaceful\n"
+ "sailboat\n"
+ "saturday\n"
+ "shameful\n"
+ "sidewalk\n"
+ "snowball\n"
+ "splendid\n"
+ "suitcase\n"
+ "sunblock\n"
+ "sunshine\n"
+ "swimming\n"
+ "thankful\n"
+ "thinnest\n"
+ "thursday\n"
+ "whatever\n"
+ "whenever\n"
+ "windmill\n"
+ "american\n"
+ "possible\n"
+ "suddenly\n"
+ "airplane\n"
+ "alphabet\n"
+ "bathroom\n"
+ "favorite\n"
+ "medicine\n"
+ "december\n"
+ "dinosaur\n"
+ "elephant\n"
+ "February\n"
+ "football\n"
+ "forehead\n"
+ "headache\n"
+ "hospital\n"
+ "lollipop\n"
+ "november\n"
+ "outdoors\n"
+ "question\n"
+ "railroad\n"
+ "remember\n"
+ "sandwich\n"
+ "scissors\n"
+ "shoulder\n"
+ "softball\n"
+ "tomorrow\n"
+ "upstairs\n"
+ "vacation\n"
+ "restroom";
addAll(Arrays.asList(w.split("\\s+")));
}
};
答案 0 :(得分:0)
需要考虑的一些要点:
String c = existingGuesses.get(0); //trying to get the first character
获取单词列表中的第一个单词,而不是第一个单词。
要获取String中的第一个字符,您可以执行以下操作:
char c = newguess.getCharAt(0);
然后,您可以在列表中搜索以该字符开头的单词。接下来,您不想删除包含该字符的单词,您要删除以该字符开头的单词。
此外,您希望一次从迭代器中获取每个项目:
while(iterator.hasNext()){
String w = iterator.next(); //get the next word in the iterator here
//process w here
}
您似乎想要查找字符是否对应。也就是说,如果猜测是“foo”,那么你想要删除以f开头的所有单词,以及具有第二个字符o的所有单词以及具有第三个字符o的所有单词。这看起来有点奇怪,所以你可能想检查一下你的逻辑。
你的方法似乎指的是一些全局变量,这可能会引起一些混乱。