c# - Smartest way to convert an array of the form [][] to [,]? -
if have array string[][], what's smartest way convert regular multidimensional array string[,] assuming former array rectangular (not jagged)?
the approach can think of dimensions, declare multidimensional array dimensions, loop through source array , populate new array. wondering if there simpler solutions.
it's fun: totally agree that's overkill , best way normal iteration.
//example jagged array string[][] ja = new string[2][]; (int = 0; < 2; i++) ja[i] = enumerable.range(0, 3).select(k => "cell [" + + "," + k + "]").toarray(); //conversion 2d-array string[,] ka = new[] { new string[ja.length, ja[0].length] } .select(_ => new { x = _, y = ja.select((a, ia) => a.select((b, ib) => _[ia, ib] = b).count()).count() }) .select(_ => _.x) .first(); //dump result (int = 0; < 2; i++) (int k = 0; k < 3; k++) { console.writeline(ka[i, k]); } almost unreadable, works (at least when initial array not empty/malformed).
Comments
Post a Comment