เมธอด InsertRange(int index, IEnumerable<T> collection) ของ List<T> ใช้แทรกชุดข้อมูล (Collection) เข้าไปในลิสต์ที่ตำแหน่งที่กำหนด องค์ประกอบเดิมจะถูกเลื่อนไปด้านหลัง ทำได้ในครั้งเดียว ไม่ต้องวนลูป Insert หลายครั้ง
รูปแบบการใช้งาน
List list = new List { 10, 20, 30, 40, 50 };
int[] newItems = { 60, 70, 80, 90 };
list.InsertRange(5, newItems); // แทรกที่ดัชนี 5 (ท้ายลิสต์) = เพิ่มต่อท้าย
โค้ดตัวอย่างสมบูรณ์
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
List numbers = new List { 10, 20, 30, 40, 50 };
Console.WriteLine("ลิสต์ต้นฉบับ:");
Console.WriteLine(string.Join(", ", numbers));
// ข้อมูลที่จะแทรก
int[] newNumbers = { 60, 70, 80, 90 };
// แทรกที่ตำแหน่ง 5 (ท้ายลิสต์ - เทียบเท่า AddRange)
numbers.InsertRange(5, newNumbers);
Console.WriteLine("\nหลัง InsertRange(5, newNumbers):");
Console.WriteLine(string.Join(", ", numbers)); // 10, 20, 30, 40, 50, 60, 70, 80, 90
// แทรกกลางลิสต์
int[] moreNumbers = { 100, 200 };
numbers.InsertRange(2, moreNumbers); // แทรกที่ดัชนี 2 (ก่อน 30)
Console.WriteLine("\nหลัง InsertRange(2, {100, 200}):");
Console.WriteLine(string.Join(", ", numbers)); // 10, 20, 100, 200, 30, 40, 50, 60, 70, 80, 90
// แทรกที่หน้าสุด
numbers.InsertRange(0, new[] { -10, -5 });
Console.WriteLine("\nหลัง InsertRange(0, {-10, -5}):");
Console.WriteLine(string.Join(", ", numbers)); // -10, -5, 10, 20, 100, 200, 30, 40, 50, 60, 70, 80, 90
// ใช้กับ IEnumerable อื่นๆ (ไม่ใช่ Array เท่านั้น)
Console.WriteLine("\n--- แทรกจาก List อื่น ---");
List extra = new List { 999, 888 };
numbers.InsertRange(numbers.Count, extra); // เพิ่มท้าย
Console.WriteLine(string.Join(", ", numbers));
}
}
ผลลัพธ์
ลิสต์ต้นฉบับ:
10, 20, 30, 40, 50
หลัง InsertRange(5, newNumbers):
10, 20, 30, 40, 50, 60, 70, 80, 90
หลัง InsertRange(2, {100, 200}):
10, 20, 100, 200, 30, 40, 50, 60, 70, 80, 90
หลัง InsertRange(0, {-10, -5}):
-10, -5, 10, 20, 100, 200, 30, 40, 50, 60, 70, 80, 90
--- แทรกจาก List อื่น ---
-10, -5, 10, 20, 100, 200, 30, 40, 50, 60, 70, 80, 90, 999, 888
InsertRange vs AddRange vs Insert
| เมธอด | ตำแหน่ง | จำนวนรายการ | กรณีใช้งาน |
|---|---|---|---|
Add(item) | ท้ายสุด | 1 | เพิ่มรายการเดียว |
AddRange(coll) | ท้ายสุด | หลายรายการ | ต่อท้ายหลายรายการ (เร็วกว่า InsertRange ท้าย) |
Insert(index, item) | ระบุได้ | 1 | แทรก 1 รายการตรงกลาง |
InsertRange(index, coll) | ระบุได้ | หลายรายการ | แทรกหลายรายการตรงกลาง |
ประสิทธิภาพ
InsertRangeที่ตำแหน่งกลาง: O(n + m) - ต้องเลื่อนองค์ประกอบเดิม (n) และ Copy ใหม่ (m)AddRangeท้ายสุด: O(m) - ไม่ต้องเลื่อน (Amortized)- หากแทรกบ่อยครั้งที่กลาง ควรพิจารณา
LinkedList<T>แทน
การใช้งานจริง: Merge Sorted Lists
public static List MergeSorted(List list1, List list2) where T : IComparable
{
var result = new List(list1.Count + list2.Count);
int i = 0, j = 0;
while (i < list1.Count && j < list2.Count)
{
if (list1[i].CompareTo(list2[j]) <= 0)
result.Add(list1[i++]);
else
result.Add(list2[j++]);
}
// เพิ่มส่วนที่เหลือ
if (i < list1.Count) result.InsertRange(result.Count, list1.GetRange(i, list1.Count - i));
if (j < list2.Count) result.InsertRange(result.Count, list2.GetRange(j, list2.Count - j));
return result;
}
ข้อควรระวัง
indexต้องอยู่ในช่วง 0 ถึงCount(รวม Count = แทรกท้าย)- Collection ที่ส่งเข้าไปต้องไม่เป็น
null(โยนArgumentNullException) - เป็น Shallow Copy - Reference Type จะชี้ Object เดียวกัน
- การแทรกที่ดัชนี 0 บ่อยครั้ง ทำให้ประสิทธิภาพต่ำ (O(n²)) - ควลใช้
LinkedListหรือสร้างใหม่ - Capacity จะขยายอัตโนมัติหากจำเป็น