0

我需要使用 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);
    }
}

}

4

1 回答 1

0

使用 Generic.Dictionary<string,Word> 或KeyedCollection<string,Word>怎么样?
如果您使用 KeyedCollection,则需要从它继承并提供 GetKeyForItem 实现。

于 2013-04-28T20:51:17.593 回答