splinter123
splinter123

Reputation: 1203

Casting exception transforming Lists to Arrays

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

Answers (1)

Rohit Jain
Rohit Jain

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

Related Questions