激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C# - C#集合類用法實例代碼詳解

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

2022-01-25 14:04jianshu C#

本文通過實例代碼給大家介紹了C#集合類用法的相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

下面介紹C#的集合類

1ArrayList

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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ù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://www.jianshu.com/p/24940abac999?utm_source=tuicool&utm_medium=referral

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品欧美久久久久一区二区 | 欧美一级色片 | 7777网站| 国产一区二区三区在线免费 | 精国产品一区二区三区 | 精品久久久久久综合日本 | 中文字幕精品在线视频 | 日韩精品一区二 | 99精品视频99 | 精品成人久久久 | 国产91精品欧美 | 一区免费 | 久久91精品国产91久久yfo | 国产又白又嫩又紧又爽18p | 久久久www成人免费精品 | 国产精品久久久久久久久久10秀 | 久久逼网| 99re3| 欧美一级不卡视频 | 午夜视频在线看 | 最新中文字幕在线视频 | 精品一区二区久久久久久按摩 | 一级毛片免费高清 | 久久国产亚洲视频 | 宅男噜噜噜66国产免费观看 | 欧美囗交 | 亚洲尻逼视频 | 精品人伦一区二区三区蜜桃网站 | 黄色网址电影 | 在线播放一区二区三区 | 涩涩伊人 | 加勒比综合 | 蜜桃传免费看片www 日本一区二区三区视频在线 | 97中文字幕在线观看 | 欧美一区黄色 | 亚洲免费在线视频 | 中文字幕精品一二三四五六七八 | 亚洲成人免费电影 | 亚洲人成综合第一网 | wwwxxx国产 | chengrenzaixian|