java: как поместить элементы значения в массив значений hashmap? - PullRequest
0 голосов
/ 19 марта 2020

Я пытался поместить результат int после броска кубика в значение hashmap. 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]);

Спасибо за помощь

1 Ответ

0 голосов
/ 19 марта 2020

Если вы хотите сохранить серию 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); 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...