C#集合類用法實例代碼詳解

下面介紹C#的集合類

創(chuàng)新互聯(lián)建站是創(chuàng)新、創(chuàng)意、研發(fā)型一體的綜合型網(wǎng)站建設(shè)公司,自成立以來公司不斷探索創(chuàng)新,始終堅持為客戶提供滿意周到的服務(wù),在本地打下了良好的口碑,在過去的10年時間我們累計服務(wù)了上千家以及全國政企客戶,如主動防護網(wǎng)等企業(yè)單位,完善的項目管理流程,嚴(yán)格把控項目進(jìn)度與質(zhì)量監(jiān)控加上過硬的技術(shù)實力獲得客戶的一致稱揚。

1ArrayList

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace 動態(tài)數(shù)組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();
    }
  }
}

總結(jié)

以上所述是小編給大家介紹的C#集合類用法實例代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!

分享標(biāo)題:C#集合類用法實例代碼詳解
文章來源:http://bm7419.com/article18/jjccgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)動態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、、自適應(yīng)網(wǎng)站網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)