[英]How to convert an arraylist to a multidimensional array
I have an ArrayList
object which its values are arrays of Object
. 我有一个
ArrayList
对象,其值是Object
数组。 I want to convert it to an array. 我想将其转换为数组。 This is the summary of what i did:
这是我所做的摘要:
ArrayList result = new ArrayList();
Object[] row = new Object[4];
result.add(row);
Object[][] arrayResult = result.toArray();
but I get an error that i can't cast it to an Object[][]
. 但出现错误,无法将其强制转换为
Object[][]
。 What should I do? 我该怎么办?
Change it to something like these: 将其更改为以下内容:
ArrayList<Object> result = new ArrayList<Object>(); // 1
Object[] row = new Object[4];
result.add(row);
Object[][] arrayResult = result.toArray(new Object[0][0]); // 2
toArray
with no arguments would just give you a Object[]
, you need to give it an array as a "example" . toArray
只会给您一个Object[]
,您需要给它一个数组作为“示例” 。 This is caused by something called "type erasure", which you can read a bit from here .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.