c#数据结构 线性表篇 非常用线性集合总结

本人能力有限,使用了一些Ai的结论,如有不足还请斧正

目录

[1.HashSet <=> Dictionary](#1.HashSet <=> Dictionary)

[2.SortedSet <=>提供升序方法的List](#2.SortedSet <=>提供升序方法的List)

3.ArrayList<=>List

[4.BitArray <=> Bit[] array](#4.BitArray <=> Bit[] array)

[5.StringCollection <=>List](#5.StringCollection <=>List)

6.StringDictionary<=>Dictionary


1.HashSet (可用)<=> Dictionary<onlyTKey,notUseTValue>

使用途径:因为字典的查询元素:Contains 平均时间复杂度为 (O(1)

所以一些场合将其代替List 但是普通字典又是键值对形式 所以不想要Value的时候可以使用HashSet

去除表中重复元素:

cs 复制代码
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbersWithDuplicates = new List<int> { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 };
        HashSet<int> uniqueNumbers = new HashSet<int>(numbersWithDuplicates);

        foreach (int number in uniqueNumbers)
        {
            Console.WriteLine(number);
        }
    }
}

2.SortedSet (可用)<=>自己实现Sort的List

SortedSet<T> 同样是一个存储唯一元素的集合,但它会对元素进行自动排序

如果除了元素唯一性 ,你还需要集合中的元素有序 ,那么 SortedSet<T> 是不错的选择

cs 复制代码
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        SortedSet<int> sortedSet = new SortedSet<int>();
        sortedSet.Add(3);
        sortedSet.Add(1);
        sortedSet.Add(2);

        foreach (int num in sortedSet)
        {
            Console.Write(num + " ");
        }
        Console.WriteLine();
    }
}

打印结果:1 2 3

3.BitArray(重要) <=> bool[] boolArray

学习过网络的同学相信对这个东西并不陌生

BitArray 是一个专门用于存储位值的集合类,它以紧凑的方式存储布尔值,每个布尔值仅占 1 位。这意味着在存储大量布尔值时,BitArray 占用的内存空间相对较小

其 提供了丰富的位操作方法,如 And(按位与)、Or(按位或)、Xor(按位异或)、Not(按位取反)等,方便进行复杂的位运算

支持动态调整大小,可以通过 Length 属性改变其长度

cs 复制代码
using System;
using System.Collections;

class Program
{
    static void Main()
    {
        BitArray bitArray1 = new BitArray(new bool[] { true, false, true });
        BitArray bitArray2 = new BitArray(new bool[] { false, true, true });
        bitArray1.And(bitArray2); // 按位与操作
        for (int i = 0; i < bitArray1.Length; i++)
        {
            Console.WriteLine(bitArray1[i]);
        }
    }
}

4.ArrayList(淘汰)<=>List<Object>

ArrayList是非泛型集合 其存储的是Objcet 所以每次存储和获取元素时都需要进行装箱(值类型转换为 object 类型)和拆箱(object 类型转换为值类型)操作,这会带来一定的性能开销

因此,不建议使用但是仍然可以用

cs 复制代码
using System;
using System.Collections;

class Program
{
    static void Main()
    {
        ArrayList logEntries = new ArrayList();
        logEntries.Add("Operation started");
        logEntries.Add(123);
        logEntries.Add(DateTime.Now);

        foreach (object entry in logEntries)
        {
            if (entry is string)
            {
                Console.WriteLine($"String: {entry}");
            }
            else if (entry is int)
            {
                Console.WriteLine($"Integer: {entry}");
            }
            else if (entry is DateTime)
            {
                Console.WriteLine($"Date: {entry}");
            }
        }
    }
}

5.StringDictionary((淘汰))<=>Dictionary<string,string>

  • StringDictionaryStringDictionary 继承自 DictionaryBase 类,实现了 IDictionaryICollectionIEnumerable 等非泛型接口。这意味着它是一个非泛型集合,在操作时需要进行类型转换。
  • Dictionary<string, string>Dictionary<string, string> 是泛型集合,实现了 IDictionary<string, string>ICollection<KeyValuePair<string, string>>IEnumerable<KeyValuePair<string, string>> 等泛型接口。泛型的使用使得代码更加类型安全,无需进行显式的类型转换

StringDictionary 是大小写不敏感的。也就是说,在 StringDictionary 中,键 "Key""key" 被视为相同的键

cs 复制代码
using System;
using System.Collections.Specialized;

class Program
{
    static void Main()
    {
        StringDictionary stringDict = new StringDictionary();
        stringDict.Add("Key", "Value1");
        stringDict["key"] = "Value2";
        Console.WriteLine(stringDict["Key"]); // 输出: Value2
    }
}

但是也就仅限如此了

相关推荐
Bear on Toilet9 分钟前
Bug日记——实现“日期类”
开发语言·c++·bug
apcipot_rain13 分钟前
《面向对象程序设计-C++》实验五 虚函数的使用及抽象类
开发语言·c++
明月看潮生3 小时前
青少年编程与数学 02-019 Rust 编程基础 05课题、复合数据类型
开发语言·青少年编程·rust·编程与数学
幼稚诠释青春3 小时前
Java学习笔记(对象)
java·开发语言
XYR1212123 小时前
C# 参数
c#
Wyc724094 小时前
JDBC:java与数据库连接,Maven,MyBatis
java·开发语言·数据库
强化学习与机器人控制仿真4 小时前
Newton GPU 机器人仿真器入门教程(零)— NVIDIA、DeepMind、Disney 联合推出
开发语言·人工智能·python·stm32·深度学习·机器人·自动驾驶
oMMh4 小时前
使用C# ASP.NET创建一个可以由服务端推送信息至客户端的WEB应用(2)
前端·c#·asp.net
Risehuxyc4 小时前
GrassRoot备份项目
c#
芯片SIPI设计4 小时前
MIPI C-PHY 标准学习----一种通用多信号传输方案
c语言·开发语言·学习