王建设医生个人网站,网站建设业务活动,电商系统平台开发的主要技术,纯免费建站目录
一、介绍
二、数组
三、List#xff08;列表#xff09;
四、Dictionary#xff08;字典#xff09;
五、Queue#xff08;队列#xff09;
六、Stack#xff08;栈#xff09;
七、Hashtable#xff08;哈希表#xff09;
结束 一、介绍
数据结构是计…目录
一、介绍
二、数组
三、List列表
四、Dictionary字典
五、Queue队列
六、Stack栈
七、Hashtable哈希表
结束 一、介绍
数据结构是计算机存储、组织数据的方式。数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。通常情况下精心选择的数据结构可以带来更高的运行或者存储效率。数据结构往往同高效的检索算法和索引技术有关。
它们的逻辑结构通常有
1.集合数据结构中的元素之间除了“同属一个集合”的相互关系外别无其他关系 2.线性结构数据结构中的元素存在一对一的相互关系 3.树形结构数据结构中的元素存在一对多的相互关系 4.图形结构数据结构中的元素存在多对多的相互关系。 下面案例只介绍数据结构中常用的一些用法因为具体的 API 实在太多了详细的介绍页可以参考微软的官方文档
C# 教程 - 概述 | Microsoft Learn 二、数组
可以将同一类型的多个变量存储在一个数组数据结构中。 通过指定数组的元素类型来声明数组。 如果希望数组存储任意类型的元素可将其类型指定为 object。
下面是编程语言中常用的数组由于是一些基础的语法有点编程基础的人几乎都懂这里就不做过多的介绍。
int[] intArr new int[3] { 2, 4, 5 };
这里介绍一下 ArrayList ArrayList 在我们平时工作中用的非常少我写 C# 很多年几乎就没用过但还是可以了解了解
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test4
{internal class Program{static void Main(string[] args){ArrayList arr new ArrayList();//向数组中添加数据arr.Add(1);arr.Add(2);arr.Add(3);//读取数组中指定索引的值Console.WriteLine(arr[0]{0}, arr[0]);//获取数组的长度int count arr.Count;//数组中是否包含指定的值bool b arr.Contains(3);//向指定的下标插入值arr.Insert(0, 1);//移除指定的元素arr.Remove(1);//移除指定的下标对应的元素arr.RemoveAt(0);//清除数组arr.Clear();Console.ReadKey();}}
}三、List列表
表示可通过索引访问的对象的强类型列表。 提供用于对列表进行搜索、排序和操作的方法。
List 是一种强类型列表List 在大多数情况下比ArrayList 执行的更好并且是类型安全的。使用泛型集合需要先引入命名空间 using System.Collections.Generic;
之前写过自定义List 的帖子这篇帖子会写的更详细一些有兴趣的可以去了解了解
链接点击跳转
常见的用法如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test4
{internal class Program{static void Main(string[] args){Liststring list new Liststring();//向列表中添加元素list.Add(a);list.Add(b);list.Add(c);//向列表中 给指定位置插入对应的值list.Insert(0, e);//移除列表中指定的元素list.Remove(e);//移除指定的下标list.RemoveAt(0);//获取列表的长度int count list.Count;//列表是否包含指定的元素bool b list.Contains(a);//给列表指定的索引赋值list[1] f;//清空列表list.Clear();Console.ReadKey();}}
}List 的遍历
using System;
using System.Collections;
using System.Collections.Generic;namespace Test4
{internal class Program{static void Main(string[] args){Listint list1 new Listint() { 2, 3, 56, 34, 64, 23 };//for循环遍历for (int i 0; i list1.Count; i){Console.WriteLine(list1[i]);}//foreach遍历foreach (int i in list1){Console.WriteLine(i);}Console.ReadKey();}}
} 四、Dictionary字典
Dictionary 是存储键和值的集合。Dictionary 是无序的键 Key 是唯一的
常见的用法如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test4
{internal class Program{static void Main(string[] args){//创建一个字典对象//Key 的类型是stringValue 的类型是intDictionarystring, int dic new Dictionarystring, int();//Add 方法用来添加键值对dic.Add(老王,15);dic.Add(张三,34);//获取当前字典中存储的个数Console.WriteLine(字典的个数{0} dic.Count);//检查字典中是否包含指定的 Keybool isContain dic.ContainsKey(张三);//尝试获取对应的value如果返回true则代表获取value成功否则则为获取失败int value 0;bool b dic.TryGetValue(老王, out value);//通过 key 获取 valueint age dic[老王];Console.WriteLine(老王的年龄是 age);//清除字典dic.Clear();Console.ReadKey();}}
}
字典的遍历
方式1
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test4
{internal class Program{static void Main(string[] args){Dictionaryint, string dict new Dictionaryint, string(){[10] A10,[20] A20,[30] A30,[40] A40,[50] A50};foreach (var key in dict.Keys){Console.WriteLine($key{key},value{dict[key]});}Console.ReadKey();}}
}方式2
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test4
{internal class Program{static void Main(string[] args){Dictionaryint, string dict new Dictionaryint, string(){[10] A10,[20] A20,[30] A30,[40] A40,[50] A50};Listint list new Listint(dict.Keys);for (int i 0; i dict.Count; i){Console.WriteLine(key:{0}value:{1}, list[i], dict[list[i]]);}Console.ReadKey();}}
}五、Queue队列
表示对象的先进先出集合。
常见的用法如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test4
{internal class Program{static void Main(string[] args){Queuestring queue new Queuestring();//向队列中添加元素queue.Enqueue(老一);queue.Enqueue(老二);queue.Enqueue(老三);//获取队列的数量int count queue.Count;//队列中是否包含指定的 valuebool b queue.Contains(老王);//获取队列中的元素//每次调用 Dequeue 方法获取并移除队列中队首的元素string s1 queue.Dequeue();Console.WriteLine(s1);string s2 queue.Dequeue();Console.WriteLine(s2);string s3 queue.Dequeue();Console.WriteLine(s3);//清空队列queue.Clear();Console.ReadKey();}}
}六、Stack栈
表示对象的简单后进先出 (LIFO) 非泛型集合。
常见的用法如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test4
{internal class Program{static void Main(string[] args){Stackstring stack new Stackstring();//将元素入栈stack.Push(a);stack.Push(b);stack.Push(c);//栈的元素个数int count stack.Count;//是否包含指定的元素bool b stack.Contains(a);// Pop 把元素出栈栈中就没有这个元素了string s1 stack.Pop();Console.WriteLine(s1);string s2 stack.Pop();Console.WriteLine(s2);string s3 stack.Pop();Console.WriteLine(s3);Console.ReadKey();}}
}七、Hashtable哈希表
表示根据键的哈希代码进行组织的键/值对的集合。
常见的用法如下
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test4
{internal class Program{static void Main(string[] args){Hashtable ht new Hashtable();//添加keyvalue键值对ht.Add(E, e);ht.Add(A, a);ht.Add(C, c);ht.Add(B, b); //获取哈希 key 对应的 valuestring s (string)ht[A];Console.WriteLine(s);Console.WriteLine(ht[A]);//判断哈希表是否包含指定的 keybool b ht.Contains(E);//哈希表的个数int count ht.Count;//移除一对键值对ht.Remove(C);//移除所有元素ht.Clear();Console.ReadKey();}}
}哈希表的遍历
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test4
{internal class Program{static void Main(string[] args){Hashtable ht new Hashtable();//添加keyvalue键值对ht.Add(E, e);ht.Add(A, a);ht.Add(C, c);ht.Add(B, b);//遍历方法一遍历哈希表中的键foreach (string key in ht.Keys){//Console.WriteLine(string.Format({0}-{1}), key, ht[key]);Console.WriteLine(string.Format({0}-{1}, key, ht[key]));}//遍历方法二遍历哈希表中的值foreach (string value in ht.Values){Console.WriteLine(value);}//遍历方法三遍历哈希表中的键值foreach (DictionaryEntry de in ht){Console.WriteLine(string.Format({0}-{1}, de.Key, de.Value));}Console.ReadKey();}}
}结束
如果这个帖子对你有所帮助欢迎 关注 点赞 留言
end