当我尝试将Object数组强制转换为Long数组时,出现此异常。
线程“主”中的异常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;
}
}
更改
returnValues = (Long[]) hotelRooms.keySet().toArray();
至
returnValues = hotelRooms.keySet().toArray(new Long[hotelRooms.size()]);
让我知道它是否有效:-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句