简体   繁体   English

将对象Array转换为Long数组时出现ClassCastException

[英]ClassCastException when casting object Array to Long array

I get this exception when i try to cast an Object array to a Long array. 当我尝试将Object数组转换为Long数组时,我得到此异常。

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; 线程“main”中的异常java.lang.ClassCastException:[Ljava.lang.Object; cannot be cast to [Ljava.lang.Long; 不能投[Ljava.lang.Long;

My keys in my hotelRooms map are Long, why it is not possible to cast. 我在hotelRooms地图中的钥匙很长,为什么不能施展。 Does someone know how to solve this. 有人知道如何解决这个问题。

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;
}
}   

change 更改

returnValues = (Long[]) hotelRooms.keySet().toArray();

to

returnValues = hotelRooms.keySet().toArray(new Long[hotelRooms.size()]);

and let me know if it works :-) 让我知道它是否有效:-)

It's because Object[] Set.toArray() returns an object array. 这是因为Object[] Set.toArray()返回一个对象数组。 You can't downcast the array to a more specific type. 您无法将数组转发为更具体的类型。 Use <T> T[]Set.toArray(T[] a) instead. 请改用<T> T[]Set.toArray(T[] a) If the generic type method didn't exist you'd have to loop through each of the Objects in the return object array and cast each individually into a new Long array. 如果泛型类型方法不存在,则必须循环遍历返回对象数组中的每个对象,并将每个对象单独转换为新的Long数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 强制转换为对象数组时发生ClassCastException - ClassCastException when casting to object array Java中将Object []数组转换为泛型类型数组时发生ClassCastException - ClassCastException when casting Object[] array to generic type array in Java 将数组强制转换为readObject时发生ClassCastException - ClassCastException when casting array to readObject 强制转换为同一对象时发生ClassCastException - ClassCastException when casting to same object 访问Object []类型的数组时发生ClassCastException - ClassCastException when accessing array of type Object[] Spring JDBC-将对象转换为Blob时发生ClassCastException - Spring JDBC - ClassCastException when casting Object to Blob 将 Object 转换为 String 时出现 ClassCastException,但将 Object 转换为自定义类时没有 ClassCastException - ClassCastException when casting Object to String, but no ClassCastException when casting Object to Custom class 将Long强制转换为Integer会引发ClassCastException - Casting Long to Integer throws ClassCastException 投射 TargetDataLine 时出现 ClassCastException - ClassCastException when casting TargetDataLine 将 Class.forName 对象转换为此对象实现的接口时出现 ClassCastException - ClassCastException when casting Class.forName object to interface implemented by this object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM