当前位置: 首页 > web >正文

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

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

目录

1.HashSet <=> Dictionary

2.SortedSet <=>提供升序方法的List

3.ArrayList<=>List

4.BitArray <=> Bit[] array

5.StringCollection <=>List

6.StringDictionary<=>Dictionary


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

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

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

        去除表中重复元素:

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> 是不错的选择

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 属性改变其长度

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 类型转换为值类型)操作,这会带来一定的性能开销

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

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 类,实现了 IDictionaryICollection 和 IEnumerable 等非泛型接口。这意味着它是一个非泛型集合,在操作时需要进行类型转换。
  • Dictionary<string, string>Dictionary<string, string> 是泛型集合,实现了 IDictionary<string, string>ICollection<KeyValuePair<string, string>> 和 IEnumerable<KeyValuePair<string, string>> 等泛型接口。泛型的使用使得代码更加类型安全,无需进行显式的类型转换

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

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

        但是也就仅限如此了 

http://www.xdnf.cn/news/3687.html

相关文章:

  • springboot基于推荐算法的景点推荐系统(源码+lw+部署文档+讲解),源码可白嫖!
  • 20:深度学习-多层感知器原理
  • Android 端如何监控 ANR、Crash、OOM 等严重问题
  • 深度学习系统学习系列【2】之人工神经网络(ANN)
  • Javase 基础入门 —— 07 接口
  • ChatGPT:重塑人工智能交互范式的破晓之作
  • 【AI论文】交互式生成视频综述
  • 【libuv】基于libuv的exe链接错误
  • JVM——JVM 是如何处理异常的?
  • 传奇各版本迭代时间及内容变化,屠龙/嗜魂法杖/逍遥扇第一次出现的时间和版本
  • Linux53 百度网盘运行(下载devtoolset11后仍提示stdc++3.0.29缺失 计划用docker容器隔离运行,计划后续再看)
  • 鼠标悬浮特效:常见6种背景类悬浮特效
  • Docker与WSL2如何清理
  • 一篇文章看懂web服务
  • NV203NV207SSD固态闪存NV208NV213
  • NoxLucky:个性化动态桌面,打造独一无二的手机体验
  • 用docker ffmpeg测试视频vmaf分数,很快不用编译
  • C#VisionMaster算子二次开发(非方案版)
  • NocoDB:开源的 Airtable 替代方案
  • operator 可以根据需要重载 == 运算符进行比较
  • 《告别试错式开发:TDD的精准质量锻造术》
  • 【quantity】7 角度单位模块(angle.rs)
  • 电脑RGB888P转换为JPEG方案 ,K230的RGB888P转换为JPEG方案
  • CGI(Common Gateway Interface)协议详解
  • 【AI面试准备】TensorFlow与PyTorch构建缺陷预测模型
  • AtCoder AT_abc404_g [ABC404G] Specified Range Sums
  • ​​信息泄露:网站敏感文件泄漏的隐形危机与防御之道​
  • 前端面试每日三题 - Day 23
  • 泰迪杯特等奖案例学习资料:基于时空图卷积网络的城市排水系统水位精准重建与异常检测
  • Power Query精通指南2:数据转换——透视/逆透视/分组、横向纵向合并数据、条件判断、处理日期时间