在Java中创建地图 [英] Creating a map in Java
问题描述
我有:
Map< double,char> memory = new HashMap< double,char>();
但是,编译时返回错误。
错误是所有4种类型(左和右)的意外错误类型。
另一个错误说:type Map不采用参数
我如何解决这个问题?还是有更好的方法来制作字典?
谢谢David,
适应
现在的代码是:
Map< Double,Character> memory = new HashMap< Double,Character>();
它在名为GameLogic的类中。
但是,编译后会返回此错误消息:
GameLogic.java:5:类型Map不使用参数
地图< Double,Character> memory = new HashMap< Double,Character>();
扩展代码:
public class GameLogic
{
Map< Double,Character> memory = new HashMap< Double,Character>();
public static int position;
public static char currentBlock;
public static int currentGold;
public static int startingPosition;
public GameLogic()
{
currentGold = 0;
}
public void player_position()
{
...
像这样,在Java 5以上:
地图< Double,Character> memory = new HashMap< Double,Character>();
或者像这样,在Java 7及以上:
Map< Double,Character> memory = new HashMap<>();
在Java中,您不能创建基本类型的通用集合,因此您必须使用包装器类 - 所以 double
成为 Double
,而 char
成为code> Character 。
Im wanting to create a map with a float (or double) key and a char value. I have:
Map<double, char> memory = new HashMap<double, char>();
However this is returning errors when compiling. The errors are saying unexpected error type for all 4 types (left and right). Another error says: type Map does not take parameters How do I fix this? Or is there a better approach to making a dictionary? Thank you, David
Adapted The code is now:
Map<Double, Character> memory = new HashMap<Double, Character>();
It is in the class named GameLogic. But this returns this error message upon compiling:
GameLogic.java:5: type Map does not take parameters
Map<Double, Character> memory = new HashMap<Double, Character>();
Extended code:
public class GameLogic
{
Map<Double, Character> memory = new HashMap<Double, Character>();
public static int position;
public static char currentBlock;
public static int currentGold;
public static int startingPosition;
public GameLogic()
{
currentGold=0;
}
public void player_position()
{
...
Like this, in Java 5 and up:
Map<Double, Character> memory = new HashMap<Double, Character>();
Or like this, in Java 7 and up:
Map<Double, Character> memory = new HashMap<>();
In Java you can't create a generic collection of primitive types, so you must use the wrapper classes - so double
becomes Double
, and char
becomes Character
.
这篇关于在Java中创建地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!