Reputation: 1203
I would like to build an array of arrays starting from a list, but I get a casting exception. Anybody knows why? Here is the code
List<String[]> listofarray=new ArrayList<String[]>();
//...filling the list...
String[][] ob= (String[][]) listofarray.toArray();
Upvotes: 0
Views: 37
Reputation: 213223
List#toArray()
method without any argument returns an Object[]
. You need to use the overloaded generic version - List#toArray(T[])
passing String[][]
as argument. Then you wouldn't need to cast the result back.
String[][] ob= listofarray.toArray(new String[listofarray.size()][]);
Upvotes: 5