Tương tự bài trước, gộp hai mảng thành List theo kiểu xen kẽ.
int[] array1 = { 56, 70, 77 };
int[] array2 = { 80, 99, 180 };
Ví dụ hoàn chỉnh
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int[] array1 = { 56, 70, 77 };
int[] array2 = { 80, 99, 180 };
var list = new List<int>();
for (int i = 0; i < array1.Length; i++)
{
list.Add(array1[i]);
list.Add(array2[i]);
}
int[] array3 = list.ToArray();
foreach (int res in array3)
{
Console.WriteLine(res);
}
}
}
Kết quả
56 80 70 99 77 180
Chú ý: Đây là gộp xen kẽ (interleave), không phải merge sorted. Nếu mảng khác độ dài, vòng lặp nên dùng Math.Min(array1.Length, array2.Length) và xử lý phần dư.