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 在我们平时工作中用的非常少,但还是可以了解了解

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 的类型是int
            Dictionary<string, int> dic = new Dictionary<string, int>();

            //Add 方法用来添加键值对
            dic.Add("老王",15);
            dic.Add("张三",34);

            //获取当前字典中存储的个数
            Console.WriteLine("字典的个数:{0}" + dic.Count);

            //检查字典中是否包含指定的 Key
            bool isContain = dic.ContainsKey("张三");

            //尝试获取对应的value,如果返回true,则代表获取value成功,否则则为获取失败
            int value = 0;
            bool b = dic.TryGetValue("老王", out value);

            //通过 key 获取 value
            int age = dic["老王"];
            Console.WriteLine("老王的年龄是:" + age);

            //清除字典
            dic.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)
        {
            Dictionary<int, string> dict = new Dictionary<int, string>()
            {
                [10] = "A10",
                [20] = "A20",
                [30] = "A30",
                [40] = "A40",
                [50] = "A50"
            };

            //方式1
            foreach (var key in dict.Keys)
            {
                Console.WriteLine($"key={key},value={dict[key]}");
            }

            //方式2
            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;

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;
            //队列中是否包含指定的 value
            bool b = queue.Contains("老王");

            //Peek方法是返回顶部的对象而不将其从堆栈中移除
            string names = queue.Peek();

            //获取队列中的元素
            //每次调用 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;

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

            //Stack.Peek() 方法返回顶部的对象而不将其从堆栈中移除
            string name = stack.Peek();

            // 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 对应的 value
            string s = (string)ht["A"];
            Console.WriteLine(s);
            Console.WriteLine(ht["A"]);

            //判断哈希表是否包含指定的 key
            bool 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

猜你喜欢

转载自blog.csdn.net/qq_38693757/article/details/129042919