What is the best way to convert a list into an array of type int[][]
?
List<List<int>> lst = new List<List<int>>();
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.
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.
int[][] arrays = lst.Select(a => a.ToArray()).ToArray();
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 for
s.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With