ภาพรวม
ArrayList.SetRange(int index, ICollection c) คัดลอกข้อมูลจากคอลเลกชันไปแทนที่ช่วงใน ArrayList เริ่มที่ดัชนีที่ระบุ
ตัวอย่างที่ 1: แทนที่ 2 สมาชิกแรกด้วยอาร์เรย์สตริง
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
ArrayList arrList = new ArrayList();
arrList.Add("A");
arrList.Add("B");
arrList.Add("C");
arrList.Add("D");
Console.WriteLine("ArrayList elements...");
for (int i = 0; i < arrList.Count; i++)
{
Console.WriteLine("" + arrList[i]);
}
string[] str = { "Demo", "Text" };
arrList.SetRange(0, str);
Console.WriteLine("After copying...");
for (int i = 0; i < arrList.Count; i++)
{
Console.WriteLine("" + arrList[i]);
}
}
}
ผลลัพธ์
ArrayList elements... A B C D After copying... Demo Text C D
ตัวอย่างที่ 2: แทนที่สมาชิกที่ 3-4 (ดัชนี 2) ด้วยอาร์เรย์สตริง
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
ArrayList arrList = new ArrayList();
arrList.Add("One");
arrList.Add("Two");
arrList.Add("Three");
arrList.Add("Four");
arrList.Add("Five");
arrList.Add("Six");
arrList.Add("Seven");
arrList.Add("Eight");
Console.WriteLine("ArrayList elements...");
for (int i = 0; i < arrList.Count; i++)
{
Console.WriteLine("" + arrList[i]);
}
string[] str = { "Demo", "Text" };
arrList.SetRange(2, str);
Console.WriteLine("After copying...");
for (int i = 0; i < arrList.Count; i++)
{
Console.WriteLine("" + arrList[i]);
}
}
}
ผลลัพธ์
ArrayList elements... One Two Three Four Five Six Seven Eight After copying... One Two Demo Text Five Six Seven Eight
สรุป
SetRange(index, collection)แทนที่ข้อมูลใน ArrayList เริ่มที่ index- จำนวนข้อมูลที่แทนที่ = จำนวนข้อมูลในคอลเลกชันต้นทาง
- คอลเลกชันต้นทางต้อง implement
ICollection(เช่น array, List)