C#中的集合类及其使用

集合类是用于数据存储和检索的专门类。这些类支持栈、队列、列表和哈希表等数据结构。大多数集合类实现了相同的接口,提供了一种灵活的方式来处理数据。

集合类的作用

集合类的主要作用包括:

  • 动态内存分配:允许在运行时根据需要增加或减少元素。
  • 按索引访问:通过索引访问列表中的项目。
  • 提供多种数据结构:如数组、哈希表、栈和队列。

常见的集合类及其用法

以下是 System.Collections 命名空间中常用的集合类及其描述和用法:

1. ArrayList

ArrayList 表示一个可以按索引单独访问的对象的有序集合。

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        ArrayList arrayList = new ArrayList();
        arrayList.Add("第一项");
        arrayList.Add("第二项");
        arrayList.Add("第三项");
        
        Console.WriteLine("ArrayList中的元素:");
        foreach (var item in arrayList)
        {
            Console.WriteLine(item);
        }
    }
}
2. Hashtable

Hashtable 使用键来访问集合中的元素。

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        Hashtable hashtable = new Hashtable();
        hashtable["A"] = 1;
        hashtable["B"] = 2;
        hashtable["C"] = 3;
        
        Console.WriteLine("Hashtable中的元素:");
        foreach (DictionaryEntry entry in hashtable)
        {
            Console.WriteLine($"{entry.Key}: {entry.Value}");
        }
    }
}
3. SortedList

SortedList 使用键和索引来访问列表中的项目。

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        SortedList sortedList = new SortedList();
        sortedList.Add("B", 2);
        sortedList.Add("A", 1);
        sortedList.Add("C", 3);
        
        Console.WriteLine("SortedList中的元素:");
        foreach (DictionaryEntry entry in sortedList)
        {
            Console.WriteLine($"{entry.Key}: {entry.Value}");
        }
    }
}
4. Stack

Stack 表示后进先出(LIFO)的对象集合。

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        Stack stack = new Stack();
        stack.Push("第一项");
        stack.Push("第二项");
        stack.Push("第三项");
        
        Console.WriteLine("Stack中的元素:");
        while (stack.Count > 0)
        {
            Console.WriteLine(stack.Pop());
        }
    }
}
5. Queue

Queue 表示先进先出(FIFO)的对象集合。

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        Queue queue = new Queue();
        queue.Enqueue("第一项");
        queue.Enqueue("第二项");
        queue.Enqueue("第三项");
        
        Console.WriteLine("Queue中的元素:");
        while (queue.Count > 0)
        {
            Console.WriteLine(queue.Dequeue());
        }
    }
}
6. BitArray

BitArray 表示使用0和1的值的二进制表示数组。

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        BitArray bitArray = new BitArray(5);
        bitArray[0] = true;  // 设置第一个位置为1
        bitArray[1] = false; // 设置第二个位置为0
        bitArray[2] = true;  // 设置第三个位置为1
        
        Console.WriteLine("BitArray中的元素:");
        foreach (bool bit in bitArray)
        {
            Console.WriteLine(bit ? 1 : 0);
        }
    }
}

C#的集合类提供了一种强大的方式来管理和操作数据。这些类各具特色,可以根据不同的需求选择合适的集合类来实现高效的数据存储和访问。无论是使用 ArrayList 进行动态数组操作,还是使用 Hashtable 和 SortedList 进行键值对存储,C#的集合类都能满足开发者的多种需求。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值