Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Xóa một dải phần tử khỏi ArrayList trong C#

Để xóa một dải (range) các phần tử khỏi ArrayList trong C#, bạn sử dụng phương thức RemoveRange(int index, int count). Phương thức này xóa count phần tử bắt đầu từ vị trí index.

Ví dụ 1: Xóa 2 phần tử từ chỉ mục 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("Các phần tử trong 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("\nCác phần tử trong ArrayList2...");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("ArrayList1 bằng ArrayList2? = " + list1.Equals(list2));

        // Xóa 2 phần tử bắt đầu từ chỉ mục 3 (xóa "D" và "E")
        list2.RemoveRange(3, 2);

        Console.WriteLine("\nCác phần tử trong ArrayList2 sau khi xóa dải...");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }
    }
}

Kết quả

Các phần tử trong ArrayList1...
A
B
C
D
E
F
G
H
I

Các phần tử trong ArrayList2...
A
B
C
D
E
F
G
H
I
ArrayList1 bằng ArrayList2? = False

Các phần tử trong ArrayList2 sau khi xóa dải...
A
B
C
F
G
H
I

Ví dụ 2: Xóa 6 phần tử từ chỉ mục 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("Các phần tử trong 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("\nCác phần tử trong ArrayList2...");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }

        // Xóa 6 phần tử từ chỉ mục 1 (xóa "Two" đến "Seven")
        list2.RemoveRange(1, 6);

        Console.WriteLine("\nCác phần tử trong ArrayList2 sau khi xóa dải...");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }
    }
}

Kết quả

Các phần tử trong ArrayList1...
A
B
C
D
E
F
G
H
I

Các phần tử trong ArrayList2...
One
Two
Three
Four
Five
Six
Seven

Các phần tử trong ArrayList2 sau khi xóa dải...
One

Cú pháp và tham số

public virtual void RemoveRange(int index, int count);
Tham số Mô tả
index
count Số lượng phần tử cần xóa

Exception có thể ném

  • ArgumentOutOfRangeException: index < 0 hoặc count < 0
  • ArgumentException: index + count > Count (vượt quá số phần tử)

Lưu ý quan trọng

⚠️ ArrayList đã lỗi thời - Microsoft khuyến nghị sử dụng List<T> (generic) thay thế cho type-safe và hiệu năng tốt hơn.

Tương đương với List<T>

using System.Collections.Generic;

List<string> list = new List<string> { "A", "B", "C", "D", "E" };
list.RemoveRange(1, 2); // Xóa "B", "C" -> Kết quả: A, D, E

// List<T> cũng có RemoveAll(Predicate<T>) để xóa theo điều kiện
list.RemoveAll(x => x.StartsWith("A"));

Các phương thức xóa khác của ArrayList

Phương thức Mô tả
Remove(object) Xóa lần xuất hiện đầu tiên của đối tượng
RemoveAt(int index) Xóa phần tử tại chỉ mục cụ thể
RemoveRange(int index, int count) Xóa dải phần tử
Clear() Xóa tất cả phần tử