Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert List<List<int>> to an array of arrays

Tags:

arrays

c#

What is the best way to convert a list into an array of type int[][]?

List<List<int>> lst = new List<List<int>>();
like image 796
Shlomi Komemi Avatar asked Jun 04 '11 17:06

Shlomi Komemi


People also ask

Can you convert a list to an array?

The best and easiest way to convert a List into an Array in Java is to use the . toArray() method. Likewise, we can convert back a List to Array using the Arrays. asList() method.

Can we convert int to array in Java?

Just use % 10 to get the last digit and then divide your int by 10 to get to the next one. int temp = test; ArrayList<Integer> array = new ArrayList<Integer>(); do{ array. add(temp % 10); temp /= 10; } while (temp > 0); This will leave you with ArrayList containing your digits in reverse order.


3 Answers

int[][] arrays = lst.Select(a => a.ToArray()).ToArray(); 
like image 65
Alex Bagnolini Avatar answered Sep 19 '22 22:09

Alex Bagnolini


It's easy with LINQ:

lst.Select(l => l.ToArray()).ToArray() 

If you really wanted two-dimentional array (int[,], not int[][]), that would be more difficult and the best solution would probably be using nested fors.

like image 22
svick Avatar answered Sep 20 '22 22:09

svick


you can easily do it using linq.

int[][] arrays = lst.Select(a => a.ToArray()).ToArray();

but if you want another way you can loop through the list and manually generate the 2d array.

how to loop through nested list

like image 31
Chamika Sandamal Avatar answered Sep 23 '22 22:09

Chamika Sandamal