java:如何将value元素放入hashmap值数组中



我试图将掷骰子后的int结果放入哈希图的值中。IDE在以下行显示错误:

map.put(1, diceRoll);

其解释是:

The method put(Integer, ArrayList<Integer>[]) in the type Map<Integer,ArrayList<Integer>[]> is not applicable for the arguments (int, ArrayList<Integer>)

我的代码是:

Map<Integer, ArrayList<Integer>[]> map = new HashMap<Integer, ArrayList<Integer>[]>();
ArrayList<Integer> diceRoll= new ArrayList<Integer>();
Dice dice = new Dice();
diceRoll.add(dice.getLastRoll());
map.put(1, diceRoll); 
ArrayList<Integer>[] integers = map.get(1);
System.out.print(integers[0]);

感谢的帮助

如果您想存储一系列ints,您不需要列表数组,只需要一个列表:

Map<Integer, List<Integer>> map = new HashMap<>();
List<Integer> diceRoll = new ArrayList<>();
Dice dice = new Dice();
diceRoll.add(dice.getLastRoll());
map.put(1, diceRoll); 

最新更新