我正在努力解决这个哈希表问题(Java 新手)。程序接收事件 ADD_info 的实例,然后运行“public ADD_info()”方法。对于每个实例,我想将键/值对写入哈希表(键:TASK.value,值:INFO_add.value - 两个变量都用 ADD_info 事件在外部填充)。我可以添加一个条目,但是当我输入第二个条目时,第一个条目的值会被覆盖。下面给出了简化的代码,以及程序输出。
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 数组和哈希表的条目如下所示:
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"
它显示了第一个键 TASK1 的值被覆盖。
任何帮助将不胜感激!另外,请原谅一些命名约定——其中一些是由 FBDK 自动生成的。
问候卡雷尔