Thursday, 15 May 2014

c# - Converting [][] to [] by extracting 1 element from the inner array -



c# - Converting <T>[][] to <T>[] by extracting 1 element from the inner array -

is there potential 1 liner allows me create new 1 dimensional array index of inner array of 2d?

example take first element of each inner array:

double[][] array2d = new double[10][] // inner arrays double[5] double[] array1d = new double[10]; (int i=0; i<array2d.length; i++) { array1d[i] = array2d[i][0]; }

i'd utilize linq. won't "avoid loops" in terms of execution, it'll avoid loop in your source code:

// 1darray isn't valid identifier... var singlearray = jaggedarray.select(x => x[0]).toarray();

note relies on beingness jagged array (an array of arrays). not expect true multi-dimensional (rectangular) arrays.

or more efficiently:

var singlearray = array.convertall(jaggedarray, x => x[0]);

that's more efficient because knows output size start with, , builds array straight - it's bit less idiomatic using linq these days (which more applicable all sequences, not arrays).

c# arrays multidimensional-array jagged-arrays

No comments:

Post a Comment