C# 各种集合

本文详细介绍了C#中的各种集合类型,包括数组、ArrayList、List、LinkedList、Queue、Stack、HashSet、SortedSet、HashTable、Dictionary、SortedDictionary和SortedList。文章重点讲述了这些集合的特点和应用场景,如数组的固定长度、链表的增删优势、哈希集合的去重功能等,并提供了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、前期准备

二、各类集合概念

三、数组

1、Array -- 数组

2、ArrayList -- 数组列表

3、 List -- 列表 -- (推荐)

四、链表

1、 LinkedList -- 链表

2、 Queue -- 队列 -- (先进先出)

3、Stack -- 栈 -- (先进后出)

五、集合

1、 HashSet -- 哈希集合 -- (去重)

2、SortedSet -- 排序集合 -- (去重、排序)

六、Key-Value

1、 HashTable -- 哈希表

2、 Dictionary -- 字典 -- (推荐)

3、 SortedDictionary -- 排序字典 -- (排序)

4、SortedList -- 排序列表 -- 排序 -- 重复Key不可用Add


一、前期准备

引入using:

using System;
using System.Collections;
using System.Collections.Generic;

二、各类集合概念

----------------数组----------------

Array -- 数组
    内存连续分配,
    数据类型一样,
    可以坐标访问,
    读取快,增删慢,长度固定
    
ArrayList -- 数组列表
    内存连续分配,
    数据类型不一样,任何类型做object处理
    可以坐标访问,
    长度可变
    
List -- 列表
    本质是ArrayList
    内存连续分配
    长度可变
    读取快,增删慢

----------------链表----------------

LinkedList -- 链表
    元素不连续分配,
    节点值可以重复
    找元素只能遍历,查找不方便
    增删比较方便
    
Queue -- 队列
    本质是链表
    元素不连续分配
    先进先出
    
Stack -- 栈
    本质是链表
    先进后出

----------------集合----------------

HashSet -- 哈希表
    内容分散
    数据不可重复(去重)
    适用于:统计IP,好友合并
    
SortedSet -- 排序表
    内容分散
    数据自动排序,
    数据不重复(去重)

----------------键值对----------------

HashTable -- 哈希表
    key-value 类型
    体积动态添加
    将key计算一个地址,然后放入key - value
    查找时,若地址与数据可以不对,则+1查找
    查:一次定位 增删:一次定位 
    增删改查快
    浪费空间,数据太多,重复定位,效率会降低
    
Dictionary -- 字典
    key-value 类型
    本质是HashTable
    增删改查快
    输出按插入的顺序
    
SortedDictionary -- 排序字典
    key-value 类型
    本质是HashTable
    增删改查快
    自动排序,输出
    
 SortedList -- 排序列表
    重复的key,用Add会报错

三、数组

1、Array -- 数组

    内存连续分配
    数据类型一样,
    可以坐标访问
    读取快,增删慢,长度固定

 代码编写: 

internal class Program
{
    static void Main(string[] args)
    {
        //Array配置
        int[] intArray = new int[2];

        //Array 使用
        intArray[0] = 1;

        string[] stringArray = new string[] { "abc", "def" };

        //输出显示
        for (int i = 0;i < intArray.Length; i++)
        {
            Console.WriteLine(intArray[i]);
        }
        Console.WriteLine(stringArray[0]);
        Console.WriteLine(stringArray[1]);
    }
}

2、ArrayList -- 数组列表

    内存连续分配,
    数据类型不一样,任何类型做object处理
    可以坐标访问
    长度可变

 代码编写:

internal class Program
{
    static void Main(string[] args)
    {
        ArrayList arrayList = new ArrayList();

        //添加
        arrayList.Add("abc");
        arrayList.Add(1);
        arrayList.Add(3.4f);

        //修改
        arrayList[2] = 5.4f;

        //删除
        arrayList.Remove("abc");
        arrayList.RemoveAt(0);
        //arrayList.Clear(); //列表清空

        //显示
        Console.WriteLine(arrayList[0]);
    }
}

3、 List -- 列表 -- (推荐)

    本质是ArrayList
    内存连续分配
    长度可变
    读取快,增删慢

 代码编写:

internal class Program
{
    static void Main(string[] args)
    {
        List<string> list = new List<string>();

        //增
        list.Add("aa");
        list.Add("bb");
        list.Add("cc");
        list.Add("dd");

        //修改
        list[3] = "ee";

        //删
        list.Remove("bb");
        list.RemoveAt(0);
        //list.Clear(); //列表清空

        list.ForEach(x => Console.WriteLine(x));
    }
}

四、链表

1、 LinkedList -- 链表

    元素不连续分配,
    节点值可以重复
    找元素只能遍历,查找不方便
    增删比较方便

  代码编写:

internal class Program
{
    static void Main(string[] args)
    {
        LinkedList<string> list = new LinkedList<string>();

        //添加
        list.AddFirst("a");
        list.AddLast("b");
        list.AddLast("c");
        list.AddLast("d");

        //添加
        LinkedListNode<string> node = list.Find("a");
        list.AddBefore(node, "a1");
        list.AddAfter(node, "a2");

        //删除
        list.Remove(node);
        list.Remove("b");
        list.RemoveLast();

        //是否包含 "c"
        Console.WriteLine(list.Contains("c"));

        //遍历显示
        foreach(string item in list)
        {
            Console.WriteLine(item);
        }
    }
}

2、 Queue -- 队列 -- (先进先出)

    本质是链表
    元素不连续分配
    先进先出

  代码编写:

internal class Program
{
    static void Main(string[] args)
    {
        Queue<string> queue = new Queue<string>();

        //入列
        queue.Enqueue("aa");
        queue.Enqueue("bb");
        queue.Enqueue("cc");
        queue.Enqueue("dd");

        //出列
        Console.WriteLine($"DequeueValue:{queue.Dequeue()}");
        Console.WriteLine($"nextValue:{queue.Peek()}");

        //遍历显示
        foreach(string item in queue)
        {
            Console.WriteLine(item);
        }
    }
}

3、Stack -- 栈 -- (先进后出)

    本质是链表
    先进后出 

  代码编写:

internal class Program
{
    static void Main(string[] args)
    {
        Stack<string> stack = new Stack<string>();

        //入栈
        stack.Push("aa");
        stack.Push("bb");
        stack.Push("cc");
        stack.Push("dd");
        stack.Push("ee");

        //出栈
        Console.WriteLine($"PopValue: {stack.Pop()}");
        Console.WriteLine($"nextValue: {stack.Peek()}");

        //遍历显示
        foreach(string s in stack)
        {
            Console.WriteLine(s);
        }
    }
}

五、集合

1、 HashSet -- 哈希集合 -- (去重)

    内容分散
    数据不可重复(去重
    适用于:统计IP,好友合并

   代码编写:

internal class Program
{
    static void Main(string[] args)
    {
        HashSet<string> hashSet = new HashSet<string>();

        //添加
        hashSet.Add("a");
        hashSet.Add("b");
        hashSet.Add("c");
        hashSet.Add("d");
        hashSet.Add("e");

        hashSet.Add("a");

        //删除
        hashSet.Remove("b");

        HashSet<string> hashSet_1 = new HashSet<string>();
        hashSet_1.Add("a");
        hashSet_1.Add("b");
        hashSet_1.Add("x");
        hashSet_1.Add("y");

        ////并集 -- 值保存至hastSet中
        //hashSet.UnionWith(hashSet_1);

        ////补集 -- 值保存至hashSet中
        //hashSet.SymmetricExceptWith(hashSet_1);

        ////差集 -- 值保存至hashSet中
        //hashSet.ExceptWith(hashSet_1);

        ////交集 -- 值保存至hashSet中
        //hashSet.IntersectWith(hashSet_1);

    }
}

2、SortedSet -- 排序集合 -- (去重、排序)

    内容分散
    数据自动排序,
    数据不重复(去重

   代码编写:

internal class Program
{
    static void Main(string[] args)
    {
        SortedSet<string> sortedSet = new SortedSet<string>();

        //添加
        sortedSet.Add("x");
        sortedSet.Add("a");
        sortedSet.Add("t");
        sortedSet.Add("c");

        sortedSet.Add("a");

        //删除
        sortedSet.Remove("x");

        //遍历显示
        foreach(string s in sortedSet)
        {
            Console.WriteLine(s);
        }

    }
}

六、Key-Value

1、 HashTable -- 哈希表

    key-value 类型
    体积动态添加
    将key计算一个地址,然后放入key - value
    查找时,若地址与数据可以不对,则+1查找
    查:一次定位 增删:一次定位 
    增删改查快
    浪费空间,数据太多,重复定位,效率会降低

   代码编写:

internal class Program
{
    static void Main(string[] args)
    {
        Hashtable hashtable = new Hashtable();
        hashtable.Add(1, "aa");
        hashtable.Add(2, "aa");
        hashtable[34] = true;

        foreach(DictionaryEntry item in hashtable)
        {
            Console.WriteLine(item.Key + ": " + item.Value);
        }

        //线程安全
        Hashtable.Synchronized(hashtable);
    }
}

2、 Dictionary -- 字典 -- (推荐)

    key-value 类型
    本质是HashTable
    增删改查快
    输出按插入的顺序

   代码编写:

internal class Program
{
    static void Main(string[] args)
    {
        Dictionary<int,string> dictionary = new Dictionary<int,string>();
        dictionary.Add(12, "aa");
        dictionary.Add(3, "bb");
        dictionary[123] = "cc";

        foreach (KeyValuePair<int, string> item in dictionary)
        {
            Console.WriteLine($"{item.Key} : {item.Value}");
        }
    }
}

3、 SortedDictionary -- 排序字典 -- (排序)

    key-value 类型
    本质是HashTable
    增删改查快
    自动排序,输出

   代码编写:

internal class Program
{
    static void Main(string[] args)
    {
        SortedDictionary<int,string> sortedDictionary = new SortedDictionary<int,string>();

        sortedDictionary.Add(32, "234");
        sortedDictionary.Add(2, "oo");
        sortedDictionary[80] = "ww";

        foreach (var item in sortedDictionary)
        {
            Console.WriteLine($"{item.Key} : {item.Value}");
        }
    }
}

4、SortedList -- 排序列表 -- 排序 -- 重复Key不可用Add

key-value 类型
本质是HashTable
增删改查快
自动排序,输出
重复的key,用Add会报错

   代码编写:

internal class Program
{
    static void Main(string[] args)
    {
        SortedList<int, string> sortedList = new SortedList<int, string>();

        sortedList.Add(32, "234");
        sortedList.Add(2, "oo");
        sortedList[80] = "ww";

        foreach (KeyValuePair<int, string> item in sortedList)
        {
            Console.WriteLine($"{item.Key} : {item.Value}");
        }

        IList<int> keyList = sortedList.Keys;
        IList<string> valueList = sortedList.Values;

    }
}

如有错误,烦请批评指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值