简体   繁体   中英

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();

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()][]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM