数据结构一

Arry类型

Array

概念

在内存上连续分配的,而且元素类型是一样的。可以坐标访问  读取快--增删慢,长度不变。

我们如果声明了一个数组对象,那么它会出现在堆里面,在堆里面为会这个数组对象开辟一个内存,然后放入多个元素。

 

语法

 Int[]arry=new int[3]//可以直接给定长度,声明后长度不变

 arry[0]=1;//按照索引赋值

 Int []arry1=new int[]{1,2,3}//也可以直接给定元素

 

ArrayList

概念

不定长的,连续分配的。元素没有类型限制,任何元素都是当成object处理,如果是值类型,会有装箱操作

 

语法

  ArrayList arrayList = new ArrayList();

 arrayList.Add("嘿嘿");

 arrayList.Add("Is");

 arrayList.Add(32);//add增加长度

 //arrayList[4] = 26;//索引赋值,不会增加长度(会报错)

 //删除数据

 var value = arrayList[2];

 arrayList.RemoveAt(0);//指定索引移除

 arrayList.Remove("嘿嘿");//指定元素移除

 

 

List

概念

也是Array,内存上都是连续摆放;不定长;泛型,保证类型安全,避免装箱拆箱.

读取快--增删慢

 

语法

  List<int> intList = new List<int>() { };

  intList.Add(123);

  intList.Add(123);

 

 

链表类型

LinkedList

概念

泛型的特点;链表,元素不连续分配,每个元素都有记录前后节点

节点值可以重复,找元素就只能遍历  查找不方便,增删 就比较方便。

 

语法

 LinkedList<int> linkedList = new LinkedList<int>();

 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();

 

Queue

概念

就是链表  先进先出  放任务延迟执行

 

语法

 Queue<string> numbers = new Queue<string>();

 numbers.Enqueue("one");

 numbers.Enqueue("two");

 numbers.Enqueue("three");

 numbers.Enqueue("four");

 numbers.Enqueue("four");//可以添加重复元素

 numbers.Enqueue("five");

 

 foreach (string number in numbers)

 {

     Console.WriteLine(number);

 }

 

 Console.WriteLine($"Dequeuing '{numbers.Dequeue()}'");//获取第一个元素并从列表中移除

 Console.WriteLine($"Peek at next item to dequeue: { numbers.Peek()}");//获取第一个元素

 Console.WriteLine($"Dequeuing '{numbers.Dequeue()}'");

 

 Queue<string> queueCopy = new Queue<string>(numbers.ToArray());

 foreach (string number in queueCopy)

 {

     Console.WriteLine(number);

 }

 

 Console.WriteLine($"queueCopy.Contains(\"four\") = {queueCopy.Contains("four")}");

 queueCopy.Clear();

 Console.WriteLine($"queueCopy.Count = {queueCopy.Count}");

 

 

Stack

概念

就是链表  先进后出,先产生的数据后使用.

 

语法

 Stack<string> numbers = new Stack<string>();

 numbers.Push("one");

 numbers.Push("two");

 numbers.Push("three");

 numbers.Push("four");

 numbers.Push("five");//放进去

 

 foreach (string number in numbers)

 {

     Console.WriteLine(number);

 }

 

 Console.WriteLine($"Pop '{numbers.Pop()}'");//获取并移除

 Console.WriteLine($"Peek at next item to dequeue: { numbers.Peek()}");//获取不移除

 Console.WriteLine($"Pop '{numbers.Pop()}'");

 

 Stack<string> stackCopy = new Stack<string>(numbers.ToArray());

 foreach (string number in stackCopy)

 {

     Console.WriteLine(number);

 }

 

 Console.WriteLine($"stackCopy.Contains(\"four\") = {stackCopy.Contains("four")}");

 stackCopy.Clear();

 Console.WriteLine($"stackCopy.Count = {stackCopy.Count}");

 

 

 

Set

HashSet

概念

集合:hash分布,元素间没关系,动态增加容量  去重。应用场景:统计用户IPIP投票   交叉并补,应用场景:二次好友/间接关注/粉丝合集

 

语法

 HashSet<string> hashSet = new HashSet<string>();

                hashSet.Add("123");

                hashSet.Add("12435");

                hashSet.Add("12435");

                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("12435");

                    hashSet1.Add("12435");

                    hashSet1.SymmetricExceptWith(hashSet);//

                    hashSet1.UnionWith(hashSet);//

                    hashSet1.ExceptWith(hashSet);//

                    hashSet1.IntersectWith(hashSet);//

                }

                hashSet.ToList();

                hashSet.Clear();

 

            }

 

SortedSet

 概念

排序的集合:去重  而且排序  

 

语法

 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

Hashtable

概念

Hashtable key-value  体积可以动态增加 拿着key计算一个地址,然后放入key - value

object-装箱拆箱  如果不同的key得到相同的地址,第二个在前面地址上 + 1

查找的时候,如果地址对应数据的key不对,那就 + 1查找。Hashtable是基于数组实现

查找个数据  一次定位; 增删 一次定位;  增删查改 都很快

如果数据太多,重复定位定位,效率就下去了

 

语法

     Hashtable table = new Hashtable();

                table.Add("123", "456");

                table[234] = 456;

                table[234] = 567;

                table[32] = 4562;

                table[1] = 456;

                table["eleven"] = 456;

                foreach (DictionaryEntry objDE in table)

                {

                    Console.WriteLine(objDE.Key.ToString());

                    Console.WriteLine(objDE.Value.ToString());

                }

                //线程安全

                Hashtable.Synchronized(table);//只有一个线程写  多个线程读

 

Dictionary

概念

字典:泛型;key - value,增删查改 都很快;有序的,字典不是线程安全

 ConcurrentDictionary(线程安全)

 

语法

                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

 

语法

 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}");

 }

 

 

 

 SortedList

语法

 SortedList sortedList = new SortedList();//IComparer

 sortedList.Add("First", "Hello");

 sortedList.Add("Second", "World");

 sortedList.Add("Third", "!");

 

 sortedList["Third"] = "~~";//

 sortedList.Add("Fourth", "!");

 sortedList.Add("Fourth", "!");//重复的Key Add会错

 sortedList["Fourth"] = "!!!";

 var keyList = sortedList.GetKeyList();

 var valueList = sortedList.GetValueList();

 

 sortedList.TrimToSize();//用于最小化集合的内存开销

 

 sortedList.Remove("Third");

 sortedList.RemoveAt(0);

 sortedList.Clear();

猜你喜欢

转载自www.cnblogs.com/JohnTang/p/11048968.html