简体   繁体   English

java:如何将值元素放入哈希图值数组?

[英]java: how to put value elements into a hashmap value array?

I was trying to put the int result after rolling a dice into the value of a hashmap.我试图在掷骰子后将 int 结果放入哈希图的值中。 IDE shows error on this line: IDE 在这一行显示错误:

map.put(1, diceRoll);

its explaination is:它的解释是:

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

My code is:我的代码是:

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]);

Thank you for the help感谢您的帮助

If you want to store a series of int s, you don't need an array of lists, just a list:如果要存储一系列int ,则不需要列表数组,只需一个列表:

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

map.put(1, diceRoll); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM