【发布时间】:2012-05-09 04:48:41
【问题描述】:
// Demonstrate a Hashtable
import java.util.*;
class HTDemo {
public static void main(String args[]) {
Hashtable balance = new Hashtable();
Enumeration names;
String str;
double bal;
balance.put("John Doe", new Double(3434.34));
balance.put("Tom Smith", new Double(123.22));
balance.put("Jane Baker", new Double(1378.00));
balance.put("Todd Hall", new Double(99.22));
balance.put("Ralph Smith", new Double(-19.08));
// Show all balances in hash table.
names = balance.keys();
while(names.hasMoreElements()) {
str = (String) names.nextElement();
System.out.println(str + ": " +
balance.get(str));
}
System.out.println();
// Deposit 1,000 into John Doe's account
***bal = ((Double)balance.get("John Doe")).doubleValue();***
balance.put("John Doe", new Double(bal+1000));
System.out.println("John Doe's new balance: " +
balance.get("John Doe"));
}
}
- 在线
bal = ((Double)balance.get("John Doe")).doubleValue();doubleValue有什么用? (我知道它将对象转换为双值)但是如果我没有这个程序运行,程序运行正常。 - (如果我错了,请纠正我)
balance.get在这里获取一个值3434.34的双对象,并且它前面的 (double ) 进行拆箱并将其转换为双值的双对象,然后如何以及为什么 @987654326 @ 将这个双重3434.34视为对象??????
【问题讨论】:
-
请多花一点时间来格式化你的问题,这样你会得到更好的帮助。
-
你考虑过使用HashMap
()吗?它更快(不同步),泛型可以让你避免类型转换。
标签: java casting hashtable type-conversion unboxing