[英]How can we add data values to hashmap<String, ArrayList<String>>
public class EvilHangman { 公共课程EvilHangman {
public Map<String, ArrayList<String>> hashmap = new HashMap<String, ArrayList<String>>();
public Map<String, ArrayList<String>> hashmappattern = new HashMap<String, ArrayList<String>>();
public ArrayList<String> wordlist = new ArrayList<String>();
private String basekey="";
/*
public EvilHangman(){
hashmap = new HashMap<String, ArrayList<String>>();
wordlist= new ArrayList<String>();
}
*/
public void readDictionary(String filename, int wordLength) throws IOException{
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
PrintWriter writer = new PrintWriter("temp.txt", "utf-8");
String line = br.readLine();
while (line!=null){
if (wordLength == line.length()){
writer.println(line);
wordlist.add(line);
}
line = br.readLine();
}
for (int i=0;i<wordLength;i++){
basekey+="_";
}
System.out.println(basekey+"length"+basekey.length());
hashmap.put(basekey, wordlist);
writer.close();
br.close();
}
public boolean checkLetter(String aletter){
for (int i=0;i<wordlist.size();i++){
char[] key = new char[wordlist.size()];
for (int j=0;j<wordlist.get(i).length();j++){
if (wordlist.get(i).charAt(j)==aletter.charAt(0)){
key[j] = wordlist.get(i).charAt(j);
}else{
key[j] = '_';
}
}
String wordkey = String.valueOf(key);
System.out.println("Key = "+wordkey);
if (hashmappattern.containsKey(wordkey)){
hashmappattern.get(wordkey).add(wordlist.get(i));
}else{
hashmappattern.put(wordkey, wordlist.get(i));
}
}
}
} }
I'm trying to read arraylist wordlist and for each and every word i'm scanning if entered letter (aletter) is in the word. 我正在尝试读取arraylist单词列表,并且如果单词中输入了字母(字母),则我正在扫描每个单词。 if it is there i'm putting that letter in to char array.
如果有的话,我把那个字母放到char数组中。 after going through all word I 'll have patterns like a_ , a_b , etc. Then I have take these word as a key and values into hashmap.
遍历所有单词后,我将得到a_,a_b等模式,然后将这些单词作为键并将其值放入hashmap中。 if same key is found I want to append data instead of replacing.(I have created arraylist but its not working because i cannot do put(string , string)).
如果找到相同的键,我想追加数据而不是替换。(我创建了arraylist但由于我不能做put(string,string)而无法正常工作)。 how should I solve this problem.
我应该如何解决这个问题。 I'm trying to make evilhangman using hashmap
我正在尝试使用hashmap使evilhangman
You need to create the ArrayList and fill it with the first element. 您需要创建ArrayList并用第一个元素填充它。 Then add the list to the hashmap:
然后将列表添加到哈希图中:
if (hashmappattern.containsKey(wordkey)){
hashmappattern.get(wordkey).add(wordlist.get(i));
}else{
ArrayList<String> list = new ArrayList<>();
list.add(wordList.get(i));
hashmappattern.put(wordkey, list);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.