我是新來的java和現在學習的東西,我已經嘗試了下面的程序存儲密鑰值對哈希表。我可以添加一個鍵和值,並能夠檢索它。下面的hashTable程序不工作?
public static void main(String[] args){
Hashtable balance= new Hashtable();
Enumeration names;
String str;
int a;
balance.put("vishnu",new Integer(1000));
balance.put("Sam",new Integer(1420));
balance.put("Gobi",new Integer(890));
balance.put("Vel",new Integer(50));
names=balance.keys();
while(names.hasMoreElements()){
str=(String) names.nextElement();
System.out.println(str + " is getting "+balance.get(str));
}
System.out.println();
但是,當我試圖修改增值。這給了我一個空指針異常。
a=((Integer) balance.get("vel")).intValue();
System.out.println("Before Adding "+a);
balance.put("Vel", new Integer(a+1000));
System.out.println("After adding "+balance.get("vel"));
}