Logo

C#中常见的几种集合类型

photo

2017年07月25日

Array

也就是数组。

具体表示方法是:数据类型[维数] 数组名=new 数据类型[]

举例如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[3];
            int[] b = new int[3] { 1, 2, 3 };
            int[] c = new int[] { 1, 2, 3 };
            int[,] d = new int[3,3];
        }
    }
}

ArrayList

动态数组,用法似乎跟c++的vector有点像。使用ArrayList必须引用Collections类。

声明 ArrayList a=new ArrayList();

添加

  • Add(a) 添加元素a到末尾;
  • Insert(b,a) 在位置b插入元素a;
  • InsertRange(b,a) 在位置b插入集合a;

删除

  • Remove(a) 移除元素a;
  • RemoveAt(a) 移除位置a的元素;
  • RemoveRange(a,b) 移除位置a到位置b的元素;
  • Clear() 清空;

排序 Sort();

反转 Reverse();

查找

  • IndexOf(a) 返回元素a的位置,没有则返回-1;
  • Contains(a) 检测是否含有元素a,返回true/false;

输出元素

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList a = new ArrayList();
            foreach (int i in a) Console.WriteLine(i); //不需要强制转换
            for (int i = 0; i < a.Count; i++) //与数组的Length不同
            {
                int n = (int)al2[i]; //需要强制转换
                Console.WriteLine(n);
            }
        }
    }
}

List

List类是ArrayList类的泛型等效类,它的大部分用法都与ArrayList相似。最大的区别就是在声明List集合时,我们需要同时声明List内元素的数据类型。

不过,大部分情况下,List似乎比ArrayList更加安全和高效,原因在于ArrayList会把所有插入其中的数据作为object类型来处理,所以在用ArrayList处理数据时,很可能会出现类型不匹配的错误,并且装箱和拆箱的过程会带来很大的性能耗损。关于这一点还不是很理解,会继续学习。

声明方式:

List<int> a = new List<int>();

Hashtable

传说中的哈希表。

哈希表的内部是无序散列,也就是说,其输出不是按照开始加入的顺序,但这也保证了高效率。如果以任意类型键值访问其中元素会快于其他集合,特别是当数据量特别大的时候,效率差别尤其大。如果一定要排序HashTable输出,只能自己实现。

声明:Hashtable a = new Hashtable();

Add(a,b) 在哈希表中添加键值对;
Clear() 清除哈希表中的键值对;
Contains(a) 判断哈希表中是否含有键a;
Remove(a) 删除哈希表中的键值对a;
ToString(); 返回当前Object的string;

示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable a = new Hashtable();

            a.Add(1, 1); //添加键值对
            a[1] = 2; //给指定键赋值
            //遍历1
            foreach (DictionaryEntry b in a)
            {
                Console.WriteLine("{0}{1}", b.Key, b.Value);
            }
            //遍历2
            IDictionaryEnumerator c = a.GetEnumerator();
            while (c.MoveNext())
            {
                Console.WriteLine("{0}{1}", c.Entry.Key, c.Entry.Value);
            }
            //按序输出
            ArrayList d = new ArrayList(a.Keys);
            d.Sort();
            foreach (int e in d)
            {
                Console.WriteLine(a[e]);
            }
        }
    }
}

Dictionary

Dictionary与Hashtable类似,但是Dictionary遍历的顺序就是加入的顺序。

声明:Dictionary

Add(a,b) 在字典中添加键值对;
Clear() 清除字典中的键值对;
Contains

示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> a = new Dictionary<string, string>();
            a.Add("a", "aa"); //添加键值对
            a.Add("b", "bb");
            a["a"] = "cc"; //给指定键赋值
            //用泛型结构体遍历
            foreach (KeyValuePair<string, string> b in a)
            {

                Console.WriteLine("{0}{1}", b.Key, b.Value);
            }
            //获得值集合
            foreach (string c in a.Values)
            {
                Console.WriteLine(c);
            }
        }
    }
}

Stack

后进先出。

声明:Stack a = new Stack();

Pop() 出栈;
Push(a) 进栈;
Count 获得栈包含的元素数;
Peek() 获得栈顶元素;
Contain(a) 判断栈中是否含有元素a;
Clear() 清除栈;
ToArray() 将栈复制到数组;

示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack a = new Stack();
            a.Push(1); //入栈
            a.Push(2);
            a.Pop(); //出栈
            Console.WriteLine("{0}{1}",a.Count,a.Peek()); //输出栈的元素个数
            //遍历
            foreach (int b in a)
            {
                Console.WriteLine(b);
            }
        }
    }
}

Queue

先进先出。

声明:Queue a = new Queue();

Enqueue(a) 入队;
Dequeue() 出队;
Count 获得队列包含的元素数;
Peek() 获得队列最前端元素;
Contain(a) 判断队列中是否含有元素a;
Clear() 清除队列;
ToArray() 将队列复制到数组;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue a = new Queue();
            a.Enqueue(1); //入队
            a.Enqueue(2);
            a.Dequeue(); //出队
            Console.WriteLine("{0}{1}", a.Count, a.Peek()); //输出队列的元素个数
            //遍历
            foreach (int b in a)
            {
                Console.WriteLine(b);
            }
        }
    }
}

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它
所有附件
该文章没有附件.
本文为原创文章,请注意保留出处!

留言板

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

热门文章

WordPress 后台编辑器样式实现直接预览 在WordPress3.0以后,有一个新的实用功能:你可以更改默认后台编辑器(TinyMCE)的样...WordPress后台编辑器样式实现直接预览 作者:Pastore Antonio
1533 浏览量
【干货】Chrome插件(扩展)开发全攻略 写在前面我花了将近一个多月的时间断断续续写下这篇博文,并精心写下完整demo,写博客的辛苦大家懂的...【干货】Chrome插件(扩展)开发全攻略 作者:Pastore Antonio
1488 浏览量
memcached 处理 多端口:https://blog.csdn.net/Erica_1230/article/deta...memcached处理 作者:Pastore Antonio
1481 浏览量
使用Nginx+WordPress搭建个人网站 背景很多研究技术的朋友喜欢写博客。如果希望搭建一个完全属于自己的网站,也并不困难。这里简要分享一下...使用Nginx+WordPress搭建个人网站 作者:Pastore Antonio
1453 浏览量
C#图片处理 通常对一幅图片的处理包括:格式变换,缩放(Scale),翻转(Rotate),截取(Clip),滤镜...C#图片处理 作者:Pastore Antonio
1447 浏览量