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

ลบสมาชิกที่ระบุออกจาก List ใน C#

ใน C# คลาส List<T> มีเมธอด Remove(T item) สำหรับลบ ครั้งแรกที่พบ ของค่าที่ระบุ คืนค่า true หากลบสำเร็จ false หากไม่พบค่านั้น นอกจากนี้ยังมี RemoveAt(index) ลบตามดัชนี และ RemoveAll(Predicate<T>) ลบตามเงื่อนไข

ตัวอย่างโค้ด: ลบสตริงออกจาก List

using System;
using System.Collections.Generic;

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

        Console.WriteLine("Elements in List1...");
        foreach (string res in list1)
        {
            Console.WriteLine(res);
        }

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

        Console.WriteLine("Elements in List2...");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("\nIs List2 equal to List1? = " + list2.Equals(list1));
        Console.WriteLine("\nCount of elements in list2 = " + list2.Count);

        // ลบ "US" ออกจาก list2
        list2.Remove("US");

        Console.WriteLine("\nCount of elements in list2 (updated) = " + list2.Count);
        Console.WriteLine("Elements in List2... UPDATED");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }
    }
}

ผลลัพธ์

Elements in List1...
One
Two
Three
Four
Five
Elements in List2...
India
US
UK
Canada
Poland
Netherlands
Is List2 equal to List1? = False
Count of elements in list2 = 6
Count of elements in list2 (updated) = 5
Elements in List2... UPDATED
India
UK
Canada
Poland
Netherlands

ตัวอย่างเพิ่มเติม: ลบจำนวนเต็ม

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main(string[] args)
    {
        List list = new List();
        list.Add(5);
        list.Add(10);
        list.Add(20);
        list.Add(50);
        list.Add(75);
        list.Add(100);

        Console.WriteLine("Elements in List...");
        foreach (int res in list)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("\nCount of elements in list = " + list.Count);
        list.Remove(50); // ลบค่า 50 ครั้งแรกที่พบ
        Console.WriteLine("\nCount of elements in list (updated) = " + list.Count);
        Console.WriteLine("Elements in List... UPDATED");
        foreach (int res in list)
        {
            Console.WriteLine(res);
        }
    }
}

ผลลัพธ์

Elements in List...
5
10
20
50
75
100
Count of elements in list = 6
Count of elements in list (updated) = 5
Elements in List... UPDATED
5
10
20
75
100

เมธอดลบข้อมูลใน List<T> สรุป

เมธอดคำอธิบายคืนค่าComplexity
Remove(T)ลบครั้งแรกที่พบของค่านั้นboolO(n)
RemoveAt(int index)ลบที่ดัชนีที่ระบุvoidO(n)
RemoveAll(Predicate<T>)ลบทุกตัวที่ตรงเงื่อนไขint (จำนวนที่ลบ)O(n)
RemoveRange(int index, int count)ลบช่วงของรายการvoidO(n)
Clear()ลบทั้งหมดvoidO(n)

ข้อควรระวัง

  • Remove() ลบ เฉพาะตัวแรกที่พบ หากมีค่าซ้ำหลายตัว ต้องใช้ RemoveAll() หรือวนลูป
  • การลบจากกลางลิสต์ ต้องย้ายข้อมูลต่อจากนั้นมาหน้ามา (O(n))
  • Equals() ของ List เปรียบเทียบ reference ไม่ใช่ค่า - ใช้ SequenceEqual() (LINQ) เพื่อเปรียบเทียบค่า