C#常用集合

什么是集合:集合就是一种存放多个数据的容器类型,比如最简单的集合:数组Array(完整的类型是System.Array)。

为什么需要集合:集合比数组更加强大,集合实现了更加丰富的功能,可以提高开发效率。

本篇博客介绍以下几种常用集合:

  • 动态数组:ArrayList
  • 列表:List
  • 字典:Dictionary
  • 队列:Queue
  • 栈:Stack

非泛型集合 ArrayList

优点:初始化时可以不指定长度,可以使用Count属性获取长度,随时添加Add、删除Remove,RemoveAt,访问通过索引[index]。

缺点:比数组耗费更多内存,读取速度比数组慢,ArrayList是非泛型集合,里面放的是object类,需要执行装箱拆箱操作,有性能损耗。

菜鸟教程:https://www.runoob.com/csharp/csharp-arraylist.html

泛型集合 List<T>

为避免动态数组ArrayList装箱和拆箱的问题,通常使用List<T>泛型来指定特定的类型,以减少性能损耗。

using System;
using System.Collections.Generic;
using System.Linq;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> stuList = new List<Student>();
            stuList.Add(new Student("A"));//添加元素
            stuList.Add(new Student("B"));
            stuList.Add(new Student("C"));
            stuList.Add(new Student("D"));
            var a = stuList.FirstOrDefault(t => t.name == "A");
            stuList.Remove(a);//移除指定元素
            stuList.RemoveAt(0);//根据索引移除元素
            Console.WriteLine(stuList.Contains(new Student("A")));//判断元素是否在list中
            for (int i = 0; i < stuList.Count; i++)//获取list的长度
            {
                stuList[i].PrintName();//根据索引访问
            }
            stuList.Clear();//移除所有元素
        }
    }
    public class Student
    {
        public string name;
        public Student(string name)
        {
            this.name = name;
        }
        public void PrintName()
        {
            Console.WriteLine("i am a student my name is:" + name);
        }
    }
}

输出结果:

键值对 Dictionary

键值对类型安全,没有装箱和拆箱的性能损耗,查询等操作速度快,单线程中推荐使用 Dictionary,有泛型又是,且读取速度快,容量利用更充分。对键值对的操作具体可看我的另一篇博客:https://www.cnblogs.com/gygg/p/11609166.html

栈 Stack

栈是一种先进后出,后进先出的对象集合,好比是只有一个开口的羽毛球筒,最先放进去的球,是最后一个拿出来的。

栈的常用方法:

入栈                Push()

出栈                Pop()

获取栈顶对象     Peek()

图解:

using System;
using System.Collections;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack st = new Stack();
            //入栈
            st.Push('A');
            st.Push('B');
            st.Push('C');
            st.Push('D');
            Console.Write("Current stack: ");
            foreach (char c in st)
            {
                Console.Write(c + " ");
            }
            Console.WriteLine();
            //再次添加对象
            st.Push('E');
            st.Push('F');
            //输出顶栈对象
            Console.WriteLine("The next poppable value in stack: {0}",st.Peek());
            Console.Write("Current stack: ");
            foreach (char c in st)
            {
                Console.Write(c + " ");
            }
            Console.WriteLine();
            Console.WriteLine("Removing values ");
            //出栈
            st.Pop();
            st.Pop();
            st.Pop();
            Console.Write("Current stack: ");
            foreach (char c in st)
            {
                Console.Write(c + " ");
            }
        }
    }
}

输出结果:

队列 Queue

队列是一种先进先出的对象集合,类似两端开口的羽毛球筒,先放进去的,重另一端先拿出来。

队列的常用方法

出队               Dequeue()

入队               Enqueue()

图解:

using System;
using System.Collections;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue q = new Queue();
            //入队
            q.Enqueue('A');
            q.Enqueue('B');
            q.Enqueue('C');
            q.Enqueue('D');
            Console.Write("Current queue: ");
            foreach (char c in q)
            {
                Console.Write(c + " ");
            };
            Console.WriteLine();
            //再次添加对象
            q.Enqueue('E');
            q.Enqueue('F');
            Console.Write("Current queue: ");
            foreach (char c in q)
            {
                Console.Write(c + " ");
            };
            Console.WriteLine();
            Console.WriteLine("Removing some values ");
            //出队
            char ch = (char)q.Dequeue();
            Console.WriteLine("The removed value: {0}", ch);
            ch = (char)q.Dequeue();
            Console.WriteLine("The removed value: {0}", ch);
            Console.ReadKey();
        }
    }
}

输出结果:

End!

posted @ 2020-07-21 17:01  Wahaha、  阅读(644)  评论(2编辑  收藏  举报