C#數組和集合整理


寫起來還是有些勉強的,還有很多用法沒有完全理解,只整理了一些基本點。

  • Array
  • ArrayList
  • List
  • Hashtable
  • Dictionary
  • Stack
  • Queue

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<string, string> a = Dictionary<string, string>();

Add(a,b) 在字典中添加鍵值對;
Clear() 清除字典中的鍵值對;
Contains<a,b> 判斷字典中是否含有鍵值對;
ContainsKey(a) 判斷字典中是否含有鍵a;
ContainsValue(a) 判斷字典中是否含有值a;
Remove(a) 刪除哈希表中的鍵值對a;

示例:

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);
            }
        }
    }
}

轉自:https://www.cnblogs.com/S031602240/p/6411439.html

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM