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

ลบองค์ประกอบทั้งหมดจาก ArrayList ใน C#

เมธอด Clear() ของ ArrayList ใช้ลบองค์ประกอบทั้งหมด ทำให้ Count กลายเป็น 0 แต่ Capacity ยังคงเดิม (ต้อง TrimToSize หากต้องการลด)

ตัวอย่างที่ 1: Clear และเปรียบเทียบออบเจ็กต์

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);
        }

        // list3 อ้างอิงไปที่ list2 เดียวกัน
        ArrayList list3 = list2;
        Console.WriteLine("ArrayList3 เท่ากับ ArrayList2? = " + list3.Equals(list2)); // True (Same reference)

        Console.WriteLine("จำนวนองค์ประกอบใน ArrayList2 = " + list2.Count); // 9

        // ลบทั้งหมดใน list2 (ส่งผลต่อ list3 ด้วย)
        list2.Clear();

        Console.WriteLine("จำนวนองค์ประกอบใน ArrayList2 (อัปเดต) = " + list2.Count); // 0
    }
}

ผลลัพธ์

องค์ประกอบใน ArrayList1...
A
B
C
D
E
F
G
H
I
องค์ประกอบใน ArrayList2...
A
B
C
D
E
F
G
H
I
ArrayList3 เท่ากับ ArrayList2? = True
จำนวนองค์ประกอบใน ArrayList2 = 9
จำนวนองค์ประกอบใน ArrayList2 (อัปเดต) = 0

ตัวอย่างที่ 2: ArrayList 简单

using System;
using System.Collections;

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

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

        Console.WriteLine("จำนวนองค์ประกอบใน ArrayList = " + arrList.Count); // 5

        arrList.Clear();

        Console.WriteLine("จำนวนองค์ประกอบใน ArrayList (อัปเดต) = " + arrList.Count); // 0
    }
}

ผลลัพธ์

องค์ประกอบใน ArrayList...
One
Two
Three
Four
Five
จำนวนองค์ประกอบใน ArrayList = 5
จำนวนองค์ประกอบใน ArrayList (อัปเดต) = 0

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

  • Clear() ลบรายการทั้งหมด Count = 0 แต่ Capacity ไม่เปลี่ยน (ยังจองหน่วยความจำนั้นอยู่)
  • หากต้องการปล่อยหน่วยความจำนั้น ใช้ TrimToSize() หลัง Clear
  • ArrayList เป็น Non-generic ใช้ object ทำให้มี Boxing/Unboxing และไม่ Type-safe
  • แนะนำใช้ List<T> แทน ในโค้ดใหม่ (Generic, Type-safe, ประสิทธิภาพดีกว่า)

เทียบ ArrayList vs List<T>

คุณสมบัติArrayListList<T>
Type Safetyไม่มี (object)มี (Generic)
Boxing/Unboxingมี (Value Type)ไม่มี
Clear()มีมี
Capacityมีมี
TrimToSize()มีมี

ตัวอย่าง List<T> (แนะนำ)

List<string> list = new List<string> { "One", "Two", "Three" };
Console.WriteLine(list.Count); // 3
list.Clear();
Console.WriteLine(list.Count); // 0
list.TrimExcess(); // ลด Capacity (ใน List เรียก TrimExcess ไม่ใช่ TrimToSize)