.NET数据结构的补充

  1. 集合一些知识
        //去重
        var set = new HashSet<int>() { 4, 23, 2, 4 ,5};
        //去重带排序
        var set2 = new SortedSet<int> { 4, 23, 2, 4 };

        //对集合的操作:
        //a.SymmetricExceptWith:仅包含该对象或指定集合中存在的元素(但不可同时包含两者中的元素).去除交集,剩下两个集合元素.
        //b.UnionWith:包含该对象本身和制定集合中存在的所有元素.并集
        //c.ExceptWith从当前HashSet<T> 对象中移除指定集合中的所有元素 . 差集
        //d.IntersectWith:仅包含该对象和指定集合中存在的元素.交集

2.排序列表

        //排序列表是数组和哈希表的组合。它包含一个可使用键或索引访问各项的列表。如果您使用索引访问各项,则它是一个动态数组(ArrayList),
        //如果您使用键访问各项,则它是一个哈希表(Hashtable)。集合中的各项总是按键值排序。
        SortedList s1 = new SortedList();
        s1.Add("3", "ni");
        s1.Add("2", "ai");
        s1.Add("1", "wo");

        if (s1.ContainsValue("ai"))
        {
            ICollection key = s1.Keys;
            foreach (var item in key)
            {
                print($"{item} : {s1[item]}");
            }
        }
  1. 链表使用

        LinkedList<int> a = new LinkedList<int>();
        a.AddFirst(3);
        a.AddLast(1);
        a.AddLast(4);

        LinkedListNode<int> cur = a.Find(3);
        a.AddBefore(cur, 2);
        foreach (var item in a)
        {
            print(item);
        }
  1. 集合相关的属性和方法
        List<int> k = new List<int>() { 3, 1, 2, 4 };
        print(k.Capacity);
        //有很多有趣方法自己去看了

猜你喜欢

转载自blog.csdn.net/qq_37811712/article/details/87362775