【问题标题】:Creating a Map-like structure using Array of ArrayLists使用 ArrayLists 数组创建类似 Map 的结构
【发布时间】:2013-04-28 20:38:24
【问题描述】:

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

}

【问题讨论】:

    标签: arrays list map count word


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-21
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 2022-06-15
      • 2015-11-30
      相关资源
      最近更新 更多