0

我有 2 个列表List<String> existingGuesses,其中包含字符,List<String> word = new ArrayList<String>(words)其中包含作为列表副本的单词,words如下面的代码所示。我正在尝试做的是遍历所有单词List word并遍历list existingGuesses. 然后我想将第一个字符List existingGuesses与所有单词List word逐个进行比较,如果找到匹配项,那么我想要列表中remove的单词。然后对下一个字符进行相同的处理,直到没有更多words可以比较的地方。到目前为止,我只能声明 a 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+")));
    }
};
4

1 回答 1

0

需要考虑的几点:

   String c = existingGuesses.get(0); //trying to get the first character

获取单词列表中的第一个单词,而不是第一个字符。

要获取字符串中的第一个字符,您可以执行以下操作:

  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 的单词。这似乎有点奇怪,所以你可能想检查你的逻辑。

您的方法似乎是指一些全局变量,这可能会给您带来一些困惑。

于 2013-09-23T17:20:49.640 回答