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 List trong C#

Giới thiệu

Để xóa một dải (range) các phần tử khỏi List<T> tại vị trí chỉ định, 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ừ index.

Ví dụ 1: Xóa 3 phần tử từ chỉ số 1

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main(String[] args)
    {
        List<string> list1 = new List<string>();
        list1.Add("One");
        list1.Add("Two");
        list1.Add("Three");
        list1.Add("Four");
        list1.Add("Five");

        Console.WriteLine("Các phần tử trong List1...");
        foreach (string res in list1)
            Console.WriteLine(res);

        List<string> list2 = new List<string>();
        list2.Add("India");
        list2.Add("US");
        list2.Add("UK");
        list2.Add("Canada");
        list2.Add("Poland");
        list2.Add("Netherlands");

        Console.WriteLine("Các phần tử trong List2...");
        foreach (string res in list2)
            Console.WriteLine(res);

        Console.WriteLine("\nList2 có bằng List1 không? = " + list2.Equals(list1));
        Console.WriteLine("\nSố lượng phần tử trong list2 = " + list2.Count);

        // Xóa 3 phần tử bắt đầu từ chỉ số 1 (US, UK, Canada)
        list2.RemoveRange(1, 3);

        Console.WriteLine("\nSố lượng phần tử trong list2 (đã cập nhật) = " + list2.Count);
        Console.WriteLine("Các phần tử trong List2... CẬP NHẬT");
        foreach (string res in list2)
            Console.WriteLine(res);
    }
}

Kết quả

Các phần tử trong List1...
One
Two
Three
Four
Five
Các phần tử trong List2...
India
US
UK
Canada
Poland
Netherlands
List2 có bằng List1 không? = False
Số lượng phần tử trong list2 = 6
Số lượng phần tử trong list2 (đã cập nhật) = 3
Các phần tử trong List2... CẬP NHẬT
India
Poland
Netherlands

Ví dụ 2: Xóa nhiều dải liên tiếp

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main(String[] args)
    {
        List<string> list = new List<string>();
        list.Add("India");
        list.Add("US");
        list.Add("UK");
        list.Add("Canada");
        list.Add("Poland");
        list.Add("Netherlands");
        list.Add("Bhutan");
        list.Add("Singapore");

        Console.WriteLine("Các phần tử trong List...");
        foreach (string res in list)
            Console.WriteLine(res);

        Console.WriteLine("\nSố lượng phần tử trong list = " + list.Count);

        // Xóa 3 phần tử từ chỉ số 1 (US, UK, Canada)
        list.RemoveRange(1, 3);
        Console.WriteLine("\nSố lượng phần tử (cập nhật 1) = " + list.Count);
        Console.WriteLine("Các phần tử... CẬP NHẬT 1");
        foreach (string res in list)
            Console.WriteLine(res);

        // Xóa 3 phần tử từ chỉ số 2 (Netherlands, Bhutan, Singapore)
        list.RemoveRange(2, 3);
        Console.WriteLine("\nSố lượng phần tử (cập nhật 2) = " + list.Count);
        Console.WriteLine("Các phần tử... CẬP NHẬT 2");
        foreach (string res in list)
            Console.WriteLine(res);
    }
}

Kết quả

Các phần tử trong List...
India
US
UK
Canada
Poland
Netherlands
Bhutan
Singapore
Số lượng phần tử trong list = 8
Số lượng phần tử (cập nhật 1) = 5
Các phần tử... CẬP NHẬT 1
India
Poland
Netherlands
Bhutan
Singapore
Số lượng phần tử (cập nhật 2) = 2
Các phần tử... CẬP NHẬT 2
India
Poland

Lưu ý: Nếu index hoặc count không hợp lệ (ví dụ: index < 0, count < 0, hoặc index + count > Count), phương thức sẽ ném ArgumentOutOfRangeException.