C# 数据结构
目录
一、介绍
二、数组
三、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){List<string> list = new List<string>();//向列表中添加元素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){List<int> list1 = new List<int>() { 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 的类型是string,Value 的类型是intDictionary<string, int> dic = new Dictionary<string, 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){Dictionary<int, string> dict = new Dictionary<int, 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){Dictionary<int, string> dict = new Dictionary<int, string>(){[10] = "A10",[20] = "A20",[30] = "A30",[40] = "A40",[50] = "A50"};List<int> list = new List<int>(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){Queue<string> queue = new Queue<string>();//向队列中添加元素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){Stack<string> stack = new Stack<string>();//将元素入栈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