Разбить массив на меньшие массивы - C#
Формулировка задачи:
Имееться массив чисел.
Например: mas[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21}
Вопрос: Как разбить етот массив на меншие масиви по 9 елементов?
Например: micromas[1, ] = {1,2,3,4,5,6,7,8,9} micromas[2, ] = {10,11,12,13,14,15,16,17,18}
Если елементов меньше то не трогать.
Решение задачи: «Разбить массив на меньшие массивы»
textual
Листинг программы
class Program
{
static void Main(string[] args)
{
int[] mas = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 };
int[][] micromas = new int[0][];
int count = 0;
int k = mas.Length;
for (int i = 0; i < mas.Length - 1; i++)
{
if (k> 9)
{
k = k - 9;
Array.Resize(ref micromas, micromas.Length + 1);
micromas[micromas.Length - 1] = new int[9];
Array.Copy(mas, count, micromas[i], 0, 9);
count += 9;
}
}
for (int i = 0; i < micromas.Length; i++)
{
for (int j = 0; j < micromas[i].Length; j++)
{
Console.Write($"{micromas[i][j]} ");
}
Console.WriteLine();
}
Console.Read();
}
}