对于家庭作业,我必须创建一个可以在刽子手处作弊的程序。为了做到这一点,我需要想出一种通过单词字母位置将单词分组到家庭的方法。因此,例如,如果单词长度为 4,并且他们猜测“e”,那么“- - - e”的所有单词都将位于哈希图的 Arraylist 之一中,该模式作为键,所有“- e - -" 将在另一个相同的 hashmap 的 ArrayLists 中,以模式作为键。我的问题是,尽管我的程序能够识别模式。它仍然返回一个空集或一个包含所有单词的 ArrayList。一个多小时以来,我一直在尝试不同的事情,但我似乎无法让它们正确分组。任何帮助是极大的赞赏。
import java.util.ArrayList;
import java.lang.StringBuilder;
import java.util.HashMap;
public class EvilEngine
{
HashMap<StringBuilder, ArrayList> families = new HashMap<StringBuilder, ArrayList>();
int k = 0;
ArrayList<String> currentList = new ArrayList();
StringBuilder blankPattern = new StringBuilder("");
StringBuilder newPattern = new StringBuilder("");
public void PatternMatcher(ArrayList wordlist, char guess, Integer wordlength)
{
String word;
int j = 0;
int x = 0;
int biggest = 0;
StringBuilder longest = null;
while(x < wordlist.size())
{
int i = 0;
int index = 0;
for (i=0; i < wordlength; i++)
{
blankPattern = blankPattern.append("-");
}
boolean boo = false;
newPattern = blankPattern;
word = (String) wordlist.get(x);
index = word.indexOf(guess);
while (index >= 0)
{
blankPattern.setCharAt(index, guess);
newPattern = blankPattern;
index = word.indexOf(guess, index + 1);
}
this.PatternCompiler(word,newPattern);
blankPattern = blankPattern.delete(0,wordlength);
x++;
}
}
public void PatternCompiler (String word, StringBuilder pattern)
{
if(!families.containsKey(pattern))
{
ArrayList<String> newPatternList = new ArrayList();
newPatternList.add(word);
families.put(pattern, newPatternList);
}
if (families.containsKey(pattern))
{
ArrayList<String> oldPatternList = new ArrayList();
oldPatternList = families.get(pattern);
oldPatternList.add(word);
families.put(pattern, oldPatternList);
}
else {
System.out.println("Error");
}
}
public HashMap<StringBuilder, ArrayList> returnFamilies (){
return families;
}
}