【发布时间】:2013-01-09 22:45:46
【问题描述】:
我创建了一个哈希表,并尝试使用枚举来打印出键和值。当我尝试编译代码时,我不断收到一个编译时错误,说我需要在新的双精度中放入一个“[”,我已经放入哈希表中。
编译前: Toys.put("Race Car", new double (29.99));
编译时错误说我需要这样放置:
toys.put("Race Car", new double [29.99]);
我做错了什么?
public static void main(String[] args) {
Hashtable toys = new Hashtable();
Enumeration toyName;
String getToyName;
double priceOfToy;
toys.put("Race Car", new double(29.99));
toys.put("Toy Bear", new double(15.99));
toys.put("Action Figure", new double(9.99));
//Show prices of each toy
toyName = toys.keys();
//Uses hasMoreElements method from enumeration to check what is in the hashtable
while (toyName.hasMoreElements()) {
//uses nextElement method from enumeration interface to
getToyName = (String) toyName.nextElement();
System.out.println(getToyName + "is now priced at " + toys.get(getToyName));
}
}
【问题讨论】:
-
你还在用 Java 1.4 吗?
-
我对 java 还是很陌生,我在哪里检查?
-
您不能
new原始类型。但是,由于自动装箱(从 1.5 开始),只需使用:toys.put("Minigun", 999.99)。另外,请在问题中包含确切复制粘贴错误消息。 -
我按照你说的@pst 的方式对其进行了格式化,这就是我上次编译时收到的警告:注意:/Applications/../.. 使用未经检查或不安全的操作。注意:重新编译 -Xlint:unchecked for details
-
@pst 我已经做到了,它甚至没有接近帮助我。感谢您一如既往的支持。
标签: java syntax compiler-errors