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

Xóa lần xuất hiện đầu tiên trong ArrayList C#

Giới thiệu

Phương thức Remove(object obj) của ArrayList xóa phần tử đầu tiên khớp với đối tượng cho trước (so sánh bằng Equals). Trả về void.

Ví dụ 1: Xóa từ ArrayList có phần tử trùng lặp

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");
        list2.Add("G"); // trùng
        list2.Add("I"); // trùng

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

        Console.WriteLine("Số phần tử ArrayList2 = " + list2.Count);
        list2.Remove("G"); // xóa lần đầu 'G'
        Console.WriteLine("Các phần tử ArrayList2... (CẬP NHẬT)");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }
        Console.WriteLine("Số phần tử ArrayList2 (Cập nhật) = " + list2.Count);
    }
}

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
G
I
Số phần tử ArrayList2 = 11
Các phần tử ArrayList2... (CẬP NHẬT)
A
B
C
D
E
F
H
I
G
I
Số phần tử ArrayList2 (Cập nhật) = 10

Ví dụ 2: Xóa "Tablet"

using System;
using System.Collections;

public class Demo
{
    public static void Main(string[] args)
    {
        ArrayList list = new ArrayList();
        list.Add("Laptop");
        list.Add("Dektop");
        list.Add("Wearables");
        list.Add("Tablet");
        list.Add("Notebook");
        list.Add("Ultrabooks");

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

        Console.WriteLine("Số phần tử ArrayList = " + list.Count);
        list.Remove("Tablet");
        Console.WriteLine("Các phần tử ArrayList... (CẬP NHẬT)");
        foreach (string res in list)
        {
            Console.WriteLine(res);
        }
        Console.WriteLine("Số phần tử ArrayList (Cập nhật) = " + list.Count);
    }
}

Kết quả

Các phần tử trong ArrayList...
Laptop
Dektop
Wearables
Tablet
Notebook
Ultrabooks
Số phần tử ArrayList = 6
Các phần tử ArrayList... (CẬP NHẬT)
Laptop
Dektop
Wearables
Notebook
Ultrabooks
Số phần tử ArrayList (Cập nhật) = 5

Lưu ý

  • Nếu phần tử không tồn tại, Remove không làm gì (không ném exception).
  • Để xóa tất cả các lần xuất hiện, dùng vòng lặp hoặc LINQ Where.
  • ArrayList không type-safe; ưu tiên List<T>.