java - Generic multi dimensional array conversion function -


this function converts 2-dimensional collection of strings 2-dimensional array of strings:

public static string[][] toarray(                              collection<? extends collection<string>> values){      string[][] result = new string[ values.size() ][];     int i=0;     for( collection<string> row : values ){         result[i] = row.toarray( new string[ row.size() ] );         i++;     }     return result; } 

how function written in order generically:

public static <x> x[][] arrayarray(collection<? extends collection<x>> values){     // ? } 

generics , arrays don't mix. 1 alternative pass class instance representing x , use create return array (see array.newinstance()). believe better approach pass x[][] in argument, fill it, , return it:

public static <x> x[][] toarray(collection<? extends collection<x>> values,         x[][] result) {      int = 0;     (collection<x> row : values)         result[i] = row.toarray(result[i++]);      return result; } 

for example:

list<list<integer>> l = arrays.aslist(arrays.aslist(1,2,3),                                        arrays.aslist(4,5),                                       arrays.aslist(6));  system.out.println(arrays.deeptostring(toarray(l, new integer[l.size()][0]))); 
 [[1, 2, 3], [4, 5], [6]] 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -