我正在努力解決這個哈希表問題(Java新手)。該程序接收事件ADD_info的實例,然後運行「public ADD_info()」方法。對於每一個我希望將一個鍵/值對寫入散列表的實例(鍵:TASK.value,值:INFO_add.value - 兩個變量都用ADD_info事件外部填充)。我可以添加一個條目,但是當我輸入第二個條目時,第一個條目的值條目會被覆蓋。下面給出簡化的代碼和程序輸出。新的哈希表條目覆蓋另一個鍵的值 - Java
TASK.value = "TASK1"
INFO_add_0:"1"
INFO_add_1:"2"
INFO_add_2:"3"
INFO_add_3:"4"
INFO_add_4:"5"
/** HASHTABLE ENTRIES */
No. of Keys = 1
TASK1
info_0: "1"
info_1: "2"
info_2: "3"
info_3: "4"
info_4: "5"
然後,對於第二個實例:
TASK.value = "TASK2"
INFO_add_0:"11"
INFO_add_1:"22"
INFO_add_2:"33"
INFO_add_3:"44"
INFO_add_4:"55"
/** HASHTABLE ENTRIES */
No. of Keys = 2
TASK1
info_0: "11"
info_1: "22"
info_2: "33"
info_3: "44"
info_4: "55"
TASK2
info_0: "11"
info_1: "22"
info_2: "33"
info_3: "44"
info_4: "55"
其示出的值
public class ProdInfo_SIFB extends fb.rt.FBInstance {
/** EVENT ADD_info */
public final EventInput ADD_info = new EventInput(this);
/** EVENT ADDED */
public final EventOutput ADDED = new EventOutput();
/** VAR TASK:WSTRING */
public WSTRING TASK = new WSTRING();
/** VAR INFO_add:WSTRING */
public ARRAY INFO_add = new ARRAY(new WSTRING(), 5);;
Hashtable Product_info = new Hashtable(); //instantiate hashtable
public ProdInfo_SIFB(){
super();
}
public void serviceEvent(EventServer e){
if (e == ADD_info) service_ADD_info();
}
/** Services the ADD_info event. */
public void service_ADD_info(){
System.out.println("TASK.value = " + TASK.value); //display TASK.value content
//display entries of INFO_add array as received from external source
for(int j = 0; j < INFO_add.value.length; j++){
System.out.println("INFO_add_" + j + ":" + INFO_add.value[j].toString());
}
Product_info.put(TASK.value, INFO_add.value); //add key and value to hashtable
System.out.println("No. of Keys = " + Product_info.size());
// Display all elements in hash table.
Enumeration tasks = Product_info.keys();
while(tasks.hasMoreElements()) {
String str = (String) tasks.nextElement();
ANY[] info = (ANY[]) Product_info.get(str);
System.out.println(str + ": ");
for(int i = 0; i < info.length; i++){
System.out.println("info_" + i + ": " + info[i].toString());
}
}
ADDED.serviceEvent(this);
}
}
第一個實例如下所述INFO_add陣列的條目和哈希表的外觀第一個密鑰TASK1被覆蓋。
任何幫助將不勝感激!此外,請原諒一些命名約定 - 其中一些是由FBDK自動生成的。
問候 卡雷爾
你可以提供一個[簡短的,獨立的,正確的例子](http://sscce.org),重點討論主要問題(在這種情況下向哈希表添加值) – Edd
可能最好閱讀[HashTable文檔](http://docs.oracle.com/javase/7/docs/api/java/util/Hashtable.html),你的'HashTable'按預期工作,但你想要像'HashTable >'或MultiMap –
jlordo
是的,操作員故障。 –