У меня есть карта с названем "CartoonCollection", ключи все которого — являются названиями популярных мультфильмов:
HashMap<String, Integer> CartoonCollection = new HashMap<String, Integer>();
hashMap.put("History of toys", 5); //Значения — рандомные числа, они не важны
hashMap.put("Lion king", 8);
hashMap.put("Spirited Away", 12);
hashMap.put("The beauty and the Beast", 5);
hashMap.put("Bambi", 9);
hashMap.put("Snow White and the 7 Dwarfs", 2)
Я хочу создать массив, где будут хранится все ключи CartoonCollection:
String[] massive = new String[6];
И вот, что мне удалось написать:
for (int i = 0; i<6; i++) {
for (String key : Cartoon.CartoonCollection.keySet()) {
massive[i] = key;
}
}
Когда я вывожу "massive" на консоль, то результат таков:
for (int j = 0; j<massive.length; j++) {
System.out.println(massive[j]);
}
|Output|
Snow White and the 7 Dwarfs
Snow White and the 7 Dwarfs
Snow White and the 7 Dwarfs
Snow White and the 7 Dwarfs
Snow White and the 7 Dwarfs
Snow White and the 7 Dwarfs
Что я делаю не так?
String[] massive = Cartoon.CartoonCollection.keySet().toArray()
.