我需要使用 Array 或 ArrayLists 创建一个类似 Map 的数据结构。
快速程序概览:我的地图包含 Word 对象,它是一个字符串,并包含该单词在下一个文件中出现的次数的频率计数。
这是 MyMap 的代码,程序不输出任何内容。
List<Word>[] table;
int tableSize;
int index;
public MyMap(int tableSize){
table = new ArrayList[tableSize];
this.tableSize = tableSize;
}
//Problem!!
public void put(Word w){
index = Math.abs(w.hashCode()) % tableSize;
if(table[index].isEmpty()){
table[index].add(w);
}
else{
w.increaseFreq();
table[index].set(index, w);
}
}
public void displayMap(){
for(List<Word> w: table){
System.out.println(w);
}
}
}