java - 使用 “new double”时发生编译时间错误

标签 java syntax compiler-errors

我已经创建了一个哈希表,并且试图使用枚举来打印键和值。当我尝试编译代码时,我不断收到编译时错误消息,提示我需要在散列表中放入新 double 数的'['。

编译前:
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));
  }

}

最佳答案

Map仅接受对象,不接受基元,double是基元,而Double是对象。
并考虑为您的集合使用通用类型:

Hashtable<String, Double> toys = new Hashtable<String, Double>();     
     toys.put("Race Car", new Double(29.99));
      toys.put("Toy Bear", new Double(15.99));
      toys.put("Action Figure", new Double(9.99));}

关于java - 使用 “new double”时发生编译时间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14247872/

相关文章:

ruby - 让 Ruby block /命令在没有空白 'rescue' block 的情况下静默失败

C++ 错误 : no match for call to ‘(std::string {aka std::basic_string<char>}) (std::string&)’

java - 什么时候使用嵌套的 Java 类才真正有用?

用户输入数组后出现 C++ 运行时错误

java - 从按钮获取输入并验证其正确还是错误

php - MySQL语法错误-无法创建表

c - 使用 Cuba 库编译时出错

swift - 在 Swift 4 中退出 Firebase

java - 遇到空指针异常需要第三双眼睛来协助

java - 使用数字数组来调整特定返回值的机会?