Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将对象数组强制转换为自定义类数组java_Java_Arrays - Fatal编程技术网

将对象数组强制转换为自定义类数组java

将对象数组强制转换为自定义类数组java,java,arrays,Java,Arrays,我有一个对象数组,我正试图将其转换为自定义“document”类。我尝试过以下方法: this.documentList = Arrays.copyOf(objectDocumentList, objectDocumentList.length,Document[].class); 及 for(inti=0;ii使用Gson“toJson”将对象转换为字符串函数,然后使用该字符串在网络上传递。您遇到了什么错误?自定义文档类是什么样子的?您不能在这里强制转换,您应该在类中实现一个构造函数,并使

我有一个对象数组,我正试图将其转换为自定义“document”类。我尝试过以下方法:

 this.documentList = Arrays.copyOf(objectDocumentList, objectDocumentList.length,Document[].class);


for(inti=0;ii使用Gson“toJson”将对象转换为字符串函数,然后使用该字符串在网络上传递。

您遇到了什么错误?自定义文档类是什么样子的?您不能在这里强制转换,您应该在类中实现一个构造函数,并使用数组中的数据初始化文档的新实例(例如,在类似您的for循环中)。它给出错误:java.lang.ArrayStoreException。@Mumptiz,你能给我一个例子吗?我的文档类看起来像这样:公共类文档{private String header;private String documentName;private String documentType;//getter和setter…}原因:java.lang.ClassCastException:java.lang.String无法转换为第二个选项的文档
 for(int i=0;i<this.objectDocumentList.length;i++){
     this.documentList[i]= (Document) objectDocumentList[i];
 }