简体   繁体   English

ClassCastException字符串为Object []

[英]ClassCastException String to Object[]

I am using the following code: 我正在使用以下代码:

{
    // ...
    String[] roles = new String[resultList.size()];
    int i=0;
    for (Iterator<Object[]> iter = resultList.iterator(); iter.hasNext();) {
        roles[i] = new String();
        Object[] objArr = iter.next();
        roles[i] = objArr[0].toString();
        i++;
    }
return roles;
}

However, I get a ClassCastException saying cannot cast from java.lang.String to Object[]. 但是,我得到一个ClassCastException说不能从java.lang.String转换为Object []。

try this: 尝试这个:

{
    // ...
    String[] roles = new String[resultList.size()];
    int i=0;
    for (Iterator<String> iter = resultList.iterator(); iter.hasNext();) {            
        roles[i] = iter.next();
        i++;
    }
    return roles;
}

Try this to convert an Object list to a String array: 尝试使用以下方法将Object列表转换为String数组:

    // Create an object list and add some strings to it
    List<Object> objectList = new ArrayList<>();
    objectList.add("A");
    objectList.add("B");
    objectList.add("C");

    // Create an String array with the same size of the object list
    String[] stringArray = new String[objectList.size()];

    // Iterate over the object list to fill the string array, invoking toString() in each object to get a textual representation from it
    for (int i = 0; i < objectList.size(); i++) {
        Object object = objectList.get(i);
        stringArray[i] = object.toString();
    }

    // Iterate over the string array to print the strigs
    for (String string : stringArray) {
        System.out.println(string);
    }

Can you make this: 你能做到这一点:

Object[] objArr = iter.next();

Into this: 变成这个:

  String[] objArr = (String[]) iter.next();

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

相关问题 Java中的ClassCastException错误Object [] to String [] - ClassCastException in Java error Object[] to String[] 从Object []到String []的转换给出了ClassCastException - Casting from Object[] to String[] gives a ClassCastException 将 Object 转换为 String 时出现 ClassCastException,但将 Object 转换为自定义类时没有 ClassCastException - ClassCastException when casting Object to String, but no ClassCastException when casting Object to Custom class ClassCastException: java.lang.String 不能转换为 Object - ClassCastException: java.lang.String cannot be cast to Object 入门程序员的classcastexception:从对象到String的转换 - classcastexception from an introductory programmer: conversion from object to String 无法将消息从 Json 字符串转换为对象。 类转换异常 - Not able to convert Message from Json String to Object. ClassCastException 带有扩展对象的ArrayList ClassCastException - ArrayList ClassCastException with extended object 无法将 ClassCastException 对象强制转换为 - ClassCastException Object cannot be cast to ClassCastException错误(对象和向量) - ClassCastException error ( Object and Vector ) 带有自定义对象的getParcelableExtra上的ClassCastException? - ClassCastException on getParcelableExtra with Custom Object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM