当我尝试将Object数组转换为Long数组时,出现了以下异常:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Long;
我的hotelRooms映射中的键是Long类型,为什么不能进行强制转换呢?有人知道如何解决吗?
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;
}
}