Structure 各种数据结构
//1.set集合:纯粹的容器;无需存储,就是一个容器
Array/ArrayList/List/LinkedList/Queue/Stack/HastSet/SortedSet/Hashtable/SortedList/Dictionary/SortedDictionary
IEnumerable、ICollection、IList、IQueryable
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]); } }
//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.线型结构:一对一
List:也是Array,在存储的时候;内存上都是连续摆放;不定长;泛型,保证类型安全,避免装箱拆箱; 性能也是比Arraylist要高 可用索引访问 //读取快--增删慢 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]); }
以上都可以用 索引访问 都为数组
//LinkedList:泛型的特点;链表,元素不连续分配,每个元素都有记录前后节点 不能用索引访问
非连续摆放,存储数据+地址,找数据的话就只能顺序查找,读取慢;增删快, #region 链表 { //节点值可以重复 //能不能索引访问?不能, //1.查询元素就只能遍历 查找不方便--查询慢 //2.增删 就比较方便--增删快 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(); }
//集合:hash分布,元素间没关系,动态增加容量 去重--如果是同一个引用,就可以去掉重复; //应用场景:抖音发布的作品点赞!统计用户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);
//排序的集合:去重 而且排序 //统计排名--每统计一个就丢进去集合 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条左右性能就开始下降的厉害
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()); } //线程安全 Hashtable.Synchronized(table);//只有一个线程
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}"); }
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---迭代器;就是这个迭代器,提供了一统一对线型结构数据的一种访问方式;
public IEnumerable<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