2013-04-28 75 views
0

我需要使用Array或ArrayLists创建一个类似于数据结构的地图。使用阵列列表创建一个类似于地图的结构

快速查看视图:我的地图保存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); 
    } 
} 

}

回答