fork(2) download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. static List<List<T>> toMatrix<T>(List<T> list, int elementsPerSubArray)
  8. {
  9. var matrix = new List<List<T>>();
  10. int i=0, k = -1;
  11. for ( ; i < list.Count; i++)
  12. {
  13. if (i % elementsPerSubArray == 0)
  14. {
  15. k++;
  16. matrix.Add(new List<T>());
  17. }
  18. matrix[k].Add(list[i]);
  19. }
  20. return matrix;
  21. }
  22.  
  23. public static void Main()
  24. {
  25. List<int> input = new List<int> { 1, 2, 3, 4, 5, };
  26. var result = toMatrix(input, 2);
  27. foreach (List<int> outer in result)
  28. Console.WriteLine(string.Join(
  29. ", ",
  30. outer.Select(item => item.ToString()).ToArray()
  31. ));
  32.  
  33. }
  34. }
Success #stdin #stdout 0.04s 33976KB
stdin
Standard input is empty
stdout
1, 2
3, 4
5