Structure 各种数据结构
//1.set集合:纯粹的容器;无需存储,就是一个容器
Array/ArrayList/List/LinkedList/Queue/Stack/HastSet/SortedSet/Hashtable/SortedList/Dictionary/SortedDictionary
IEnumerable、ICollection、IList、IQueryable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Array { //Array:数组 在内存上连续分配的,而且元素类型是一样的 //可以坐标访问 读取快(因为有索引)--增删慢, 长度不变,定长 Console.WriteLine( "***************Array******************" ); int [] intArray = new int [3]; intArray[0] = 123; string [] stringArray = new string [] { "123" , "234" }; //Array foreach ( var item in stringArray) { } for ( int i = 0; i < intArray.Length; i++) { Console.WriteLine(intArray[i]); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //ArrayList:以前的开发中使用的比较多 不定长的,连续分配的; //元素没有类型限制,任何元素都是当成object处理,如果是值类型,会有装箱操作 //读取快--增删慢 Console.WriteLine( "***************ArrayList******************" ); ArrayList arrayList = new ArrayList(); arrayList.Add( "Richard" ); arrayList.Add( "Is" ); arrayList.Add(32); //add增加长度 // arrayList[4] = 26;//索引复制,不会增加长度 //删除数据 //arrayList.RemoveAt(4); var value = arrayList[2]; arrayList.RemoveAt(0); arrayList.Remove( "Richard" ); foreach ( var item in arrayList) { } for ( int i = 0; i < arrayList.Count; i++) { Console.WriteLine(arrayList[i]); } |
//2.线型结构:一对一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | List:也是Array,在存储的时候;内存上都是连续摆放;不定长;泛型,保证类型安全,避免装箱拆箱; 性能也是比Arraylist要高 <strong> 可用索引访问</strong> //读取快--增删慢 Console.WriteLine( "***************List<T>******************" ); List< int > intList = new List< int >() { 1, 2, 3, 4 }; intList.Add(123); intList.Add(123); //intList.Add("123"); //intList[0] = 123; List< string > stringList = new List< string >(); //stringList[0] = "123";//异常的 foreach ( var item in intList) { } for ( int i = 0; i < intList.Count; i++) { Console.WriteLine(intList[i]); } |
以上都可以用 索引访问 都为数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //LinkedList:泛型的特点;链表,元素不连续分配,每个元素都有记录前后节点 <strong>不能用索引访问</strong>非连续摆放,存储数据+地址,找数据的话就只能顺序查找,读取慢;增删快, #region 链表 { //节点值可以重复 //能不能索引访问?不能, //1.查询元素就只能遍历 查找不方便--<strong>查询慢</strong> //2.<strong>增删 就比较方便--增删快</strong> Console.WriteLine( "***************LinkedList<T>******************" ); LinkedList< int > linkedList = new LinkedList< int >(); //linkedList[3] //不能索引访问--不是数组 linkedList.AddFirst(123); //在最前面添加 linkedList.AddLast(456); //在最后添加 bool isContain = linkedList.Contains(123); LinkedListNode< int > node123 = linkedList.Find(123); //元素123的位置 从头查找 linkedList.AddBefore(node123, 123); linkedList.AddBefore(node123, 123); linkedList.AddAfter(node123, 9); linkedList.Remove(456); linkedList.Remove(node123); linkedList.RemoveFirst(); linkedList.RemoveLast(); linkedList.Clear(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | //集合:hash分布,元素间没关系,动态增加容量 去重--如果是同一个引用,就可以<strong>去掉重复;</strong> //应用场景:抖音发布的作品点赞!统计用户IP;IP投票 //提供了一些计算:交叉并补--二次好友/间接关注/粉丝合集 //应用场景:donkey:Seven 系统可能推荐一些可能认识的人:找出Seven好友列表:找出donkey这个同学的好友列表:求差集;---是donkey的好友,但是不是Seven好友。系统就给Seven推荐:可能认识的人; Console.WriteLine( "***************HashSet<string>******************" ); HashSet< string > hashSet = new HashSet< string >(); hashSet.Add( "123" ); hashSet.Add( "689" ); hashSet.Add( "456" ); string s1 = "12345" ; hashSet.Add(s1); string s2 = "12345" ; hashSet.Add(s2); string s3 = "12345" ; hashSet.Add(s3); //hashSet[0]; foreach ( var item in hashSet) { Console.WriteLine(item); } Console.WriteLine(hashSet.Count); Console.WriteLine(hashSet.Contains( "12345" )); { HashSet< string > hashSet1 = new HashSet< string >(); hashSet1.Add( "123" ); hashSet1.Add( "689" ); hashSet1.Add( "789" ); hashSet1.Add( "12435" ); hashSet1.Add( "12435" ); hashSet1.Add( "12435" ); hashSet1.SymmetricExceptWith(hashSet); //补 hashSet1.UnionWith(hashSet); //并 hashSet1.ExceptWith(hashSet); //差 hashSet1.IntersectWith(hashSet); //交 } hashSet.ToList(); hashSet.Clear(); HashSet<People> peoples = new HashSet<People>(); People people = new People() { Id = 123, Name = "小菜" }; People people1 = new People() { Id = 123, Name = "小菜" }; peoples.Add(people); peoples.Add(people1); peoples.Add(people1); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | //排序的集合:<strong>去重 而且排序</strong> //统计排名--每统计一个就丢进去集合 Console.WriteLine( "***************SortedSet<string>******************" ); SortedSet< string > sortedSet = new SortedSet< string >(); //IComparer<T> comparer 自定义对象要排序,就用这个指定 sortedSet.Add( "123" ); sortedSet.Add( "689" ); sortedSet.Add( "456" ); sortedSet.Add( "12435" ); sortedSet.Add( "12435" ); sortedSet.Add( "12435" ); foreach ( var item in sortedSet) { Console.WriteLine(item); } Console.WriteLine(sortedSet.Count); Console.WriteLine(sortedSet.Contains( "12345" )); { SortedSet< string > sortedSet1 = new SortedSet< string >(); sortedSet1.Add( "123" ); sortedSet1.Add( "689" ); sortedSet1.Add( "456" ); sortedSet1.Add( "12435" ); sortedSet1.Add( "12435" ); sortedSet1.Add( "12435" ); sortedSet1.SymmetricExceptWith(sortedSet); //补 sortedSet1.UnionWith(sortedSet); //并 sortedSet1.ExceptWith(sortedSet); //差 sortedSet1.IntersectWith(sortedSet); //交 } sortedSet.ToList(); sortedSet.Clear(); |
key-value, 查询快,也增删快
一段连续有限空间放value(开辟的空间比用到的多,hash是用空间换性能),基于key散列计算得到地址索引,这样读取快
增删也快,删除时也是计算位置,增加也不影响别人
因为key 是最终生成了索引的;如果数量过多,散列计算后,肯定会出现不同的key计算出的索引只是同一个
肯定会出现2个key(散列冲突),散列结果一致,可以让第二次的+1,
可能会造成效率的降低,尤其是数据量大的情况下,以前测试过dictionary在3w条左右性能就开始下降的厉害
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Hashtable table = new Hashtable(); table.Add( "123" , "456" ); //table.Add("123", "456");//key相同 会报错 table[234] = 456; table[234] = 567; table[32] = 4562; table[1] = 456; table[ "Seven" ] = 456; foreach (DictionaryEntry objDE in table) { Console.WriteLine(objDE.Key.ToString()); Console.WriteLine(objDE.Value.ToString()); } <strong> //线程安全 Hashtable.Synchronized(table); //只有一个线程 </strong> |
1 2 3 4 5 6 7 8 9 10 11 12 | Dictionary< int , string > dic = new Dictionary< int , string >(); dic.Add(1, "HaHa" ); dic.Add(5, "HoHo" ); dic.Add(3, "HeHe" ); dic.Add(2, "HiHi" ); dic.Add(4, "HuHu1" ); dic[4] = "HuHu" ; //dic.Add(4, "HuHu"); foreach ( var item in dic) { Console.WriteLine($ "Key:{item.Key}, Value:{item.Value}" ); } |
1 2 3 4 5 6 7 8 9 10 11 12 | SortedDictionary< int , string > dic = new SortedDictionary< int , string >(); dic.Add(1, "HaHa" ); dic.Add(5, "HoHo" ); dic.Add(3, "HeHe" ); dic.Add(2, "HiHi" ); dic.Add(4, "HuHu1" ); dic[4] = "HuHu" ; //dic.Add(4, "HuHu"); foreach ( var item in dic) { Console.WriteLine($ "Key:{item.Key}, Value:{item.Value}" ); } |
继承了IList 为数组,,, 继承IEnumberable为集合 都可以foreach for
//数组和List<> 循环获取数据的时候,操作不一样; 还有其他的很多数据结构类型,循环获取数据的时候,都都有各自的不同;
提供一个统一的访问方式 :迭代器模式 IEnumerable中的IEnumerator 实现:IEnumerable接口就需要实现一个GetIEnumerator方法;这个方法返回的是一个Enumerator---迭代器;就是这个迭代器,提供了一统一对线型结构数据的一种访问方式;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public <strong>IEnumerable</strong>< int > Power() { for ( int i = 0; i < 10; i++) { yield return this .Get(i); Console.WriteLine( "yield这里再来一次" ); //每次有结果就返回 //yield return this.Get(i) + 1; } } public IEnumerable< int > Common() { List< int > intList = new List< int >(); for ( int i = 0; i < 10; i++) { intList.Add( this .Get(i)); Console.WriteLine( "集合这里再来一次" ); // 全部找出来以后才返回 } return intList; } |
//3.树形结构:表达式目录树(二叉树)、菜单结构:一对多
//4.图状结构(网状结构):拓扑图、网状结构(地图开发,用的上)
//ConcurrentQueue 线程安全版本的Queue
//ConcurrentStack线程安全版本的Stack
//ConcurrentBag线程安全的对象集合
//ConcurrentDictionary线程安全的Dictionary
//BlockingCollection
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现