欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

C#集合类用法实例代码详解

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

下面介绍C#的集合类

1ArrayList

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace 动态数组ArrayList
{
  class Program
  {
    static void Main(string[] args)
    {
      ArrayList a1 = new ArrayList();
      a1.Add(100);
      foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
      {
        a1.Add(number);
      }
      int[] number2 = new int[2] { 11, 12 };
      a1.AddRange(number2);
      a1.Remove(3);
      a1.RemoveAt(3);
      ArrayList al2 = new ArrayList(a1.GetRange(1,3));
      Console.WriteLine("遍历方法1:");
      foreach (int i in a1)
      {
        Console.WriteLine(i);
      }
      Console.WriteLine("遍历方法2:");
      for (int i = 0; i < al2.Count; i++)
      {
        Console.WriteLine(al2[i]);
      }
      Console.ReadLine();
    }
  }
}

2 Stack

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Stack集合类
{
  class Program
  {
    static void Main(string[] args)
    {
      Stack s1 = new Stack();
      Stack s2 = new Stack();
      foreach (int i in new int[4] { 1, 2, 3, 4 })
      {
        s1.Push(i);
        s2.Push(i);
      }
      foreach (int i in s1)
      {
        Console.WriteLine(i);
      }
      s1.Pop();
      Console.WriteLine("出栈");
      foreach (int i in s1)
      {
        Console.WriteLine(i);
      }
      int num=(int)s2.Peek();
      Console.WriteLine("弹出最后一项{0}",num);
      foreach (int i in s2)
      {
        Console.WriteLine(i);
      }
      Console.ReadLine();
    }
  }
}

3Queue

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

using System.Collections;
namespace Queue集合类
{
  class Program
  {
    static void Main(string[] args)
    {
      Queue q1 = new Queue();
      Queue q2 = new Queue();
      foreach(int i in new int [4]{1,2,3,4})
      {
        q1.Enqueue(i);
        q2.Enqueue(i);
      }
      foreach (int i in q1)
      {
        Console.WriteLine(i);
      }
      q1.Dequeue();
      Console.WriteLine("q1出队");
      foreach (int i in q1)
      {
        Console.WriteLine(i);
      }
      int num=(int)q2.Peek();
      Console.WriteLine("取q2开始处{0}",num);
      foreach(int i in q2)
      {
        Console.WriteLine(i);
      }
      Console.ReadLine();
    }
  }
}

4Hashtable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Hashtable集合类
{
  class Program
  {
    static void Main(string[] args)
    {
      Hashtable h = new Hashtable();
      h.Add("E","e");
      h.Add("B", "b");
      h.Add("C", "c");
      h.Add("A", "a");
      foreach (DictionaryEntry e in h)
      {
        Console.Write("{0},{1} ", e.Key, e.Value);
      }
      Console.WriteLine();
      string s = (string)h["C"];
      Console.WriteLine(s);
      if (h.Contains("E"))
      {
        Console.WriteLine("含有E");
      }
      Console.WriteLine(h["A"]);
      h.Remove(h["A"]);
      h.Clear();
      foreach (DictionaryEntry e in h)
      {
        Console.Write("{0},{1} ", e.Key, e.Value);
      }
      Console.ReadLine();
    }
  }
}

5SortedList

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

using System.Collections;
namespace SortedList集合类
{
  class Program
  {
    static void Main(string[] args)
    {
      SortedList s1 = new SortedList();
      s1["c"]=41;
      s1["a"]=42;
      s1["d"]=11;
      s1["b"]=13;
      foreach (DictionaryEntry e in s1)
      {
        string s = (string)e.Key;
        int i = (int)e.Value;
        Console.Write("{0},{1} ",s,i);
      }
      Console.ReadLine();
    }
  }
}

总结

以上所述是小编给大家介绍的C#集合类用法实例代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

上一篇:利用C#实现最基本的小说爬虫示例代码

栏    目:C#教程

下一篇:C#带你玩扫雷(附源码)

本文标题:C#集合类用法实例代码详解

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5445.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有