Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 2 dimensional array

What is selectMany.ToArray() method? Is it a built in method in C#?

I need to convert two dimensional array to one dimensional array.

like image 256
Manoj Avatar asked Mar 13 '09 04:03

Manoj


People also ask

How do I turn an array into a 2D array?

Use reshape() Function to Transform 1d Array to 2d Array The number of components within every dimension defines the form of the array. We may add or delete parameters or adjust the number of items within every dimension by using reshaping. To modify the layout of a NumPy ndarray, we will be using the reshape() method.

Can you change the size of a 2D array Java?

It is not possible to change the size of the same array. The closest thing you can do is create a new one either with new operator and System.

How do you convert a one-dimensional array to a two-dimensional array in C?

Call the function​ ​ input_array​ to store elements in 1D array. Call the function ​ print_array​ to print the elements of 1D array. Call the function ​ array_to_matrix​ to convert 1D array to 2D array. Call function ​ print_matrix​ to print the elements of the 2D array.


2 Answers

If you mean a jagged array (T[][]), SelectMany is your friend. If, however, you mean a rectangular array (T[,]), then you can just enumerate the date data via foreach - or:

int[,] from = new int[,] {{1,2},{3,4},{5,6}}; int[] to = from.Cast<int>().ToArray(); 
like image 179
Marc Gravell Avatar answered Oct 05 '22 07:10

Marc Gravell


SelectMany is a projection operator, an extension method provided by the namespace System.Linq.

It performs a one to many element projection over a sequence, allowing you to "flatten" the resulting sequences into one.

You can use it in this way:

int[][] twoDimensional = new int[][] {                                        new int[] {1, 2},                                       new int[] {3, 4},                                       new int[] {5, 6}                                      };  int [] flattened = twoDimensional.SelectMany(x=>x).ToArray(); 
like image 40
Christian C. Salvadó Avatar answered Oct 05 '22 07:10

Christian C. Salvadó