首页 > 编程语言 >C#的6种常用集合类

C#的6种常用集合类

时间:2023-06-07 11:35:43浏览次数:46  
标签:Trim 常用 Console C# System int WriteLine 集合 using


一.先来说说数组的不足(也可以说集合与数组的区别):

1.数组是固定大小的,不能伸缩。虽然System.Array.Resize这个泛型方法可以重置数组大小,但是该方法是重新创建新设置大小的数组,用的是旧数组的元素初始化。随后以前的数组就废弃!而集合却是可变长的

2.数组要声明元素的类型,集合类的元素类型却是object.

3.数组可读可写不能声明只读数组。集合类可以提供ReadOnly方法以只读方式使用集合。

4.数组要有整数下标才能访问特定的元素,然而很多时候这样的下标并不是很有用。集合也是数据列表却不使用下标访问。很多时候集合有定制的下标类型,对于队列和栈根本就不支持下标访问!

 

二.下面讲述6种常用集合

1.ArrayList类

C#的6种常用集合类_System

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();
            al.Add(100);//单个添加
            foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
            {
                al.Add(number);//集体添加方法一//清清月儿 
            }
            int[] number2 = new int[2] { 11,12 };
            al.AddRange(number2);//集体添加方法二
            al.Remove(3);//移除值为3的
            al.RemoveAt(3);//移除第3个
            ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取旧ArrayList一部份


            Console.WriteLine("遍历方法一:");
            foreach (int i in al)//不要强制转换
            {
                Console.WriteLine(i);//遍历方法一
            }

            Console.WriteLine("遍历方法二:");
            for (int i = 0; i != al2.Count; i++)//数组是length
            {
                int number = (int)al2[i];//一定要强制转换
                Console.WriteLine(number);//遍历方法二

            }
        }
    }
}

C#的6种常用集合类_System

C#的6种常用集合类_System_03

 

2.Stack类

栈,后进先出。push方法入栈,pop方法出栈。

 

C#的6种常用集合类_System

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack sk = new Stack();
            Stack sk2 = new Stack();
            foreach (int i in new int[4] { 1, 2, 3, 4 })
            {
                sk.Push(i);//填充
                sk2.Push(i);
            }
            
            foreach (int i in sk)
            {
                Console.WriteLine(i);//遍历
            }

            sk.Pop();
            Console.WriteLine("Pop");
            foreach (int i in sk)
            {
                Console.WriteLine(i);
            }
            
            sk2.Peek();//弹出最后一项不删除//清清月儿 
            Console.WriteLine("Peek");
            foreach (int i in sk2)
            {
                Console.WriteLine(i);
            }

            while (sk2.Count != 0)
            {
                int i = (int)sk2.Pop();//清空
                sk2.Pop();//清空
            }
            Console.WriteLine("清空");
            foreach (int i in sk2)
            {
                Console.WriteLine(i);
            }
        }
    }
}

C#的6种常用集合类_System

C#的6种常用集合类_Stack_06

 

3.Queue类

队列,先进先出。enqueue方法入队列,dequeue方法出队列。

 

C#的6种常用集合类_System

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue qu = new Queue();
            Queue qu2 = new Queue();
            foreach (int i in new int[4] { 1, 2, 3, 4 })
            {
                qu.Enqueue(i);//填充
                qu2.Enqueue(i);
            }
            
            foreach (int i in qu)
            {
                Console.WriteLine(i);//遍历
            }

            qu.Dequeue();
            Console.WriteLine("Dequeue");
            foreach (int i in qu)
            {
                Console.WriteLine(i);
            }
            
            qu2.Peek();//弹出最后一项不删除
            Console.WriteLine("Peek");
            foreach (int i in qu2)
            {
                Console.WriteLine(i);
            }

            while (qu2.Count != 0)
            {
                int i = (int)qu2.Dequeue();//清空
                qu2.Dequeue();//清空
            }
            Console.WriteLine("清空");
            foreach (int i in qu2)
            {
                Console.WriteLine(i);
            }
        }
    }
}

C#的6种常用集合类_System

C#的6种常用集合类_System_09

 

4.Hashtable类

哈希表,名-值对。类似于字典(比数组更强大)。哈希表是经过优化的,访问下标的对象先散列过。如果以任意类型键值访问其中元素会快于其他集合。GetHashCode()方法返回一个int型数据,使用这个键的值生成该int型数据。哈希表获取这个值最后返回一个索引,表示带有给定散列的数据项在字典中存储的位置。

 

C#的6种常用集合类_System

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        public static void Main()
        {

            // Creates and initializes a new Hashtable.
            Hashtable myHT = new Hashtable();
            myHT.Add("one", "The");
            myHT.Add("two", "quick");
            myHT.Add("three", "brown");
            myHT.Add("four", "fox");

            // Displays the Hashtable.//清清月儿
            Console.WriteLine("The Hashtable contains the following:");
            PrintKeysAndValues(myHT);
        }


        public static void PrintKeysAndValues(Hashtable myHT)
        {
            foreach (string s in myHT.Keys)
                Console.WriteLine(s);

            Console.WriteLine(" -KEY- -VALUE-");
            foreach (DictionaryEntry de in myHT)
                Console.WriteLine(" {0}: {1}", de.Key, de.Value);
            Console.WriteLine();
        }
    }
}

C#的6种常用集合类_System

C#的6种常用集合类_System_12

 

5.SortedList类

与哈希表类似,区别在于SortedList中的Key数组排好序的。

 

C#的6种常用集合类_System

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        public static void Main()
        {

            SortedList sl = new SortedList();
            sl["c"] = 41;
            sl["a"] = 42;
            sl["d"] = 11;
            sl["b"] = 13;

            foreach (DictionaryEntry element in sl)
            {
                string s = (string)element.Key;
                int i = (int)element.Value;
                Console.WriteLine("{0},{1}",s,i);
            }
        }
    }
}

C#的6种常用集合类_System

C#的6种常用集合类_Stack_15

 

6.NameValueCollection类

官方给NameValueCollection定义为特殊集合一类,在System.Collections.Specialized下。

System.Collections.Specialized下还有HybridDicionary类,建议少于10个元素用HybridDicionary,当元素增加会自动转为HashTable。

System.Collections.Specialized下还有HybridDicionary类,字符串集合。

System.Collections.Specialized下还有其他类大家可以各取所需!

言归正转主要说NameValueCollection,HashTable 和 NameValueCollection很类似但是他们还是有区别的,HashTable 的KEY是唯一性,而NameValueCollection则不唯一!

 

C#的6种常用集合类_System

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
namespace ConsoleApplication1
{

    class Program
    {

        static void Main(string[] args)
        {
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            ht.Add("DdpMDisplaySeq".Trim(), "Display Sequence".Trim());
            ht.Add("DdpMNameChi".Trim(), "Name (Chinese)".Trim());
            ht.Add("DdpMNameEng".Trim(), "Name (English)".Trim());
            ht.Add("Comment".Trim(), "Comment".Trim());
            ht.Add("DdpMMarketCode".Trim(), "Market Code".Trim());
            foreach (object key in ht.Keys)
            {
                Console.WriteLine("{0}/{1}    {2},{3}", key, ht[key], key.GetHashCode(), ht[key].GetHashCode());
            }
            Console.WriteLine(" ");//清清月儿
            NameValueCollection myCol = new NameValueCollection();
            myCol.Add("DdpMDisplaySeq".Trim(), "Display Sequence".Trim());
            myCol.Add("DdpMNameChi".Trim(), "Name (Chinese)".Trim());
            myCol.Add("DdpMNameChi".Trim(), "Name (English)".Trim());
            myCol.Add("Comment".Trim(), "Comment".Trim());
            myCol.Add("DdpMMarketCode".Trim(), "Market Code".Trim());
            foreach (string key in myCol.Keys)
            {
                Console.WriteLine("{0}/{1} {2},{3}", key, myCol[key], key.GetHashCode(), myCol[key].GetHashCode());
            }

        }

    }


}

C#的6种常用集合类_System


C#的6种常用集合类_System

C#的6种常用集合类_数组_19



标签:Trim,常用,Console,C#,System,int,WriteLine,集合,using
From: https://blog.51cto.com/u_4018548/6430495

相关文章

  • stm32 adc采样滤波算法
     1、简单移动平均滤波算法(SMA):采样数据作为滤波器的输入,输出为移动平均值,即取最近一段采样值的平均值作为输出。简单移动平均滤波算法实现简单,计算速度快,但只适用于信号变化缓慢的场合。//简单移动平均滤波算法#defineN10//采样点数floatFilter_Arr[N];//保存过去N个......
  • C#中委托和事件的区别
    大致来说,委托是一个类,该类内部维护着一个字段,指向一个方法。事件可以被看作一个委托类型的变量,通过事件注册、取消多个委托或方法。本篇分别通过委托和事件执行多个方法,从中体会两者的区别。 □通过委托执行方法classProgram{staticvoidMain(string[]args){Exampleexample=......
  • PCB板的Mark点设计对SMT重要性
    Mark点也称光学点、基准点,是电路板元器件组装中,PCBA应用于自动贴片机上的位置识别点。Mark点的选用,直接影响到自动贴片机的贴片效率,因此在设计时,需要设计好Mark点以及其在板内的位置。Mark点的设计1、布局位置单板Mark点在我们设计PCB时,贴片的一面需要添加Mark点,如果双面贴片则两面......
  • C++11中智能指针的原理、使用、实现
     目录理解智能指针的原理智能指针的使用智能指针的设计和实现1.智能指针的作用       C++程序设计中使用堆内存是非常频繁的操作,堆内存的申请和释放都由程序员自己管理。程序员自己管理堆内存可以提高了程序的效率,但是整体来说堆内存的管理是麻烦的,C++11中引入了智能指针的......
  • java.lang.OutOfMemoryError:GC overhead limit exceeded异常
    java.lang.OutOfMemoryError异常解决方法 原因:常见的有以下几种:1.内存中加载的数据量过于庞大,如一次从数据库取出过多数据;2.集合类中有对对象的引用,使用完后未清空,使得JVM不能回收;3.代码中存在死循环或循环产生过多重复的对象实体;4.使用的第三方软件中的BUG;5.启动参数内存......
  • 进阶篇丨链路追踪(Tracing)很简单:链路成本指南
    广义上的链路成本,既包含使用链路追踪产生的数据生成、采集、计算、存储、查询等额外资源开销,也包含链路系统接入、变更、维护、协作等人力运维成本。为了便于理解,本小节将聚焦在狭义上的链路追踪机器资源成本,人力成本将在下一小节(效率)进行介绍。链路追踪机器成本的组成结构链路追踪......
  • 关于c#:如何在不同的命名空间中处理相同的类名?
    Howtohandlesameclassnameindifferentnamespaces?我正在尝试创建一个通用的库结构。我通过为我想要的每个公共库创建单独的项目来做到这一点我有以下2个命名空间:MyCompany.ERP和MyCompany.Barcode我需要他们两个都有一个名为"Utilities"的类并且是静态的。如果这样做,我......
  • nginx 文件下载conf
    server{listen8088;server_namelocalhost;gzipon;gzip_staticon;#需要http_gzip_static_module模块gzip_min_length1k;gzip_comp_level4;gzip_proxiedany;gzip_typestext/plaintext/xmltext/css;gzip_varyon;gzip_dis......
  • 前端codeReview规范指南
    博主的写的:https://www.cnblogs.com/mrwh/p/17462559.html一、前言针对目录结构、CSS规范、JavaScript规范、Vue规范可参照官方给出的 风格指南:https://v2.cn.vuejs.org/v2/style-guide/index.html这里主要总结业务开发中常遇到的代码问题和实践,帮助大家后续各自做好codeReview,......
  • C++ 友元函数
    类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员。尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数。友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类,在这种情况下,整个类及其所有成员都是友......