Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

ลบช่วงองค์ประกอบจาก ArrayList ใน C#

เมธอด RemoveRange(int index, int count) ของ ArrayList ใช้ลบองค์ประกอบในช่วง ตั้งแต่ดัชนี index จำนวน count รายการ

ตัวอย่างที่ 1: ลบช่วง 2 รายการเริ่มจากดัชนี 3

using System;
using System.Collections;

public class Demo
{
    public static void Main(string[] args)
    {
        ArrayList list1 = new ArrayList();
        list1.Add("A");
        list1.Add("B");
        list1.Add("C");
        list1.Add("D");
        list1.Add("E");
        list1.Add("F");
        list1.Add("G");
        list1.Add("H");
        list1.Add("I");

        Console.WriteLine("องค์ประกอบใน ArrayList1...");
        foreach (string res in list1)
        {
            Console.WriteLine(res);
        }

        ArrayList list2 = new ArrayList();
        list2.Add("A");
        list2.Add("B");
        list2.Add("C");
        list2.Add("D");
        list2.Add("E");
        list2.Add("F");
        list2.Add("G");
        list2.Add("H");
        list2.Add("I");

        Console.WriteLine("องค์ประกอบใน ArrayList2...");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("ArrayList1 เท่ากับ ArrayList2? = " + list1.Equals(list2));

        // ลบ 2 รายการ เริ่มจากดัชนี 3 (คือ "D" และ "E")
        list2.RemoveRange(3, 2);

        Console.WriteLine("องค์ประกอบใน ArrayList2 หลังลบช่วง...");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }
    }
}

ผลลัพธ์

องค์ประกอบใน ArrayList1...
A
B
C
D
E
F
G
H
I
องค์ประกอบใน ArrayList2...
A
B
C
D
E
F
G
H
I
ArrayList1 เท่ากับ ArrayList2? = False
องค์ประกอบใน ArrayList2 หลังลบช่วง...
A
B
C
F
G
H
I

ตัวอย่างที่ 2: ลบช่วงใหญ่ (6 รายการจากดัชนี 1)

using System;
using System.Collections;

public class Demo
{
    public static void Main(string[] args)
    {
        ArrayList list1 = new ArrayList();
        list1.Add("A");
        list1.Add("B");
        list1.Add("C");
        list1.Add("D");
        list1.Add("E");
        list1.Add("F");
        list1.Add("G");
        list1.Add("H");
        list1.Add("I");

        Console.WriteLine("องค์ประกอบใน ArrayList1...");
        foreach (string res in list1)
        {
            Console.WriteLine(res);
        }

        ArrayList list2 = new ArrayList();
        list2.Add("One");
        list2.Add("Two");
        list2.Add("Three");
        list2.Add("Four");
        list2.Add("Five");
        list2.Add("Six");
        list2.Add("Seven");

        Console.WriteLine("องค์ประกอบใน ArrayList2...");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }

        // ลบ 6 รายการ เริ่มจากดัชนี 1 (Two ถึง Seven)
        list2.RemoveRange(1, 6);

        Console.WriteLine("องค์ประกอบใน ArrayList2 หลังลบช่วง...");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }
    }
}

ผลลัพธ์

องค์ประกอบใน ArrayList1...
A
B
C
D
E
F
G
H
I
องค์ประกอบใน ArrayList2...
One
Two
Three
Four
Five
Six
Seven
องค์ประกอบใน ArrayList2 หลังลบช่วง...
One

ข้อสังเกตสำคัญ

  • RemoveRange(index, count) ลบ count รายการ เริ่มจาก index (0-based)
  • โยน ArgumentOutOfRangeException หาก index หรือ count ไม่ถูกต้อง (ติดลบ, เกินขอบเขต, หรือ index + count > Count)
  • หลังลบ องค์ประกอบถัดไปจะเลื่อนมาทับตำแหน่ง (Shift left)
  • ArrayList เป็น Non-generic แนะนำใช้ List<T> แทนในโค้ดใหม่
  • ใน List<T> มีเมธอด RemoveRange เหมือนกัน แต่ Type-safe

ตัวอย่างเทียบเท่าใน List<T>

List<string> list = new List<string> { "A", "B", "C", "D", "E" };
list.RemoveRange(1, 2); // ลบ "B", "C" -> เหลือ A, D, E