我嘗試將對象數組強制轉換爲長陣列時出現此異常。將對象數組強制轉換爲長陣列時發生ClassCastException
異常在線程 「主」 java.lang.ClassCastException: [Ljava.lang.Object;不能投到 [Ljava.lang.Long;
我在我的hotelRooms地圖中的鑰匙很長,爲什麼它不可能施放。有人知道如何解決這個問題嗎?
public class ObjectArrayToLongArrayTest {
private Map<Long, String[]> hotelRooms;
public static void main(String[] args) {
ObjectArrayToLongArrayTest objectArrayToLongArrayTest =
new ObjectArrayToLongArrayTest();
objectArrayToLongArrayTest.start();
objectArrayToLongArrayTest.findByCriteria(null);
}
private void start() {
hotelRooms = new HashMap<Long, String[]>();
// TODO insert here some test data.
hotelRooms.put(new Long(1), new String[] {
"best resort", "rotterdam", "2", "y", "129", "12-12-2008",
"11111111"
});
hotelRooms.put(new Long(2), new String[] {
"hilton", "amsterdam", "4", "n", "350", "12-12-2009", "2222222"
});
hotelRooms.put(new Long(3), new String[] {
"golden tulip", "amsterdam", "2", "n", "120", "12-09-2009",
null
});
}
public long[] findByCriteria(String[] criteria) {
Long[] returnValues;
System.out.println("key of the hotelRoom Map" + hotelRooms.keySet());
if (criteria == null) {
returnValues = (Long[]) hotelRooms.keySet().toArray();
}
return null;
}
}
即使這個問題是關於Java而不是C++,我覺得這個討論很有用:http://www.parashift.com/c++-faq-lite/proper-inheritance.html#faq-21.4 – Jherico 2010-01-07 20:59:19