【发布时间】:2018-04-18 16:08:20
【问题描述】:
我在我的 javafx 应用程序的一种方法中使用了HashMap。此HashMap 导致以下ClassCastException:
java.lang.ClassCastException: java.util.HashMap$Node 无法转换为 java.util.ArrayList
@FXML
private void initialize() {
Map<Integer, ArrayList<WhatsOn>> movieMap = new HashMap<>();
String whatsOnString = null;
movieLabels = new Label[]{movie1, movie2, movie3, movie4, movie5, movie6};
Label[] screenLabels;
try {
//get the list of screenings from the database
whatsOnString = Harness.sendGet("whatson").toString();
WhatsOn[] whatsOn = JSON.whatsOnFromJson(whatsOnString);
//set the text of each label to the title of the movie
for (int i = 0; i < whatsOn.length; i++) {
if(!movieMap.containsKey(whatsOn[i].getMovie_ID())){
movieMap.put(whatsOn[i].getMovie_ID(),new ArrayList<>(Arrays.asList(whatsOn[i])));
} else {
ArrayList<WhatsOn> list = (ArrayList<WhatsOn>)movieMap.get(whatsOn[i].getMovie_ID());
list.add(whatsOn[i]);
System.out.println(movieMap.get(whatsOn[i].getMovie_ID()));
}
}
Iterator iterator = movieMap.entrySet().iterator();
int count = 0;
while(iterator.hasNext()){
ArrayList<WhatsOn> list = (ArrayList<WhatsOn>) iterator.next();
setMovieLabel(count, list.get(0));
for(WhatsOn whatson: list){
}
count++;
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Could not recieve data from database");
}
}
【问题讨论】:
-
替换movieMap.entrySet().iterator();用movieMap.values().iterator();
标签: java arraylist hashmap iterator classcastexception