C# System.Collections


System.collection類:

動態數組ArrayList

代表了可被單獨索引的對象的有序集合。也就是說他是一個動態的數組,你可以通過索引來進行增刪改等操作,數組會自動調整數組的大小。允許在列表進行動態內存的分配、增加、收索和排序。

Capacity:獲取或設置 ArrayList 可以包含的元素個數。
Count:獲取 ArrayList 中實際包含的元素個數。
IsFixedSize:獲取一個值,表示 ArrayList 是否具有固定大小。
IsReadOnly:獲取一個值,表示 ArrayList 是否只讀。
Item:獲取或設置指定索引處的元素。

  • public virtual int Add(object value); 在 ArrayList 的末尾添加一個對象。
  • public virtual void AddRange(ICollection c); 在 ArrayList 的末尾添加 ICollection 的元素。
  • public virtual void Clear();從 ArrayList 中移除所有的元素。
  • public virtual bool Contains(object item); 判斷某個元素是否在 ArrayList 中。
  • public virtual ArrayList GetRange(int index, int count); 返回一個ArrayList,表示源ArrayList中元素的子集。
  • public virtual int IndexOf(object);返回某個值在ArrayList中第一次出現的索引,索引從零開始。
  • public virtual void Insert(int index, object value); 在ArrayList的指定索引處,插入一個元素。
  • public virtual void InsertRange(int index, ICollection c); 在 ArrayList 的指定索引處,插入某個集合的元素。
  • public virtual void Remove(object obj); 從ArrayList中移除第一次出現的指定對象。
  • public virtual void RemoveAt(int index); 移除ArrayList的指定索引處的元素。
  • public virtual void RemoveRange(int index, int count); 從 ArrayList 中移除某個范圍的元素。
  • public virtual void Reverse();逆轉 ArrayList 中元素的順序。
  • public virtual void SetRange(int index, ICollection c); 復制某個集合的元素到 ArrayList 中某個范圍的元素上。
  • public virtual void Sort();對 ArrayList 中的元素進行排序。
  • public virtual void TrimToSize();設置容量為 ArrayList 中元素的實際個數。

哈希表hashtable

可以使用健來進行訪問、排序元素。

Count:獲取Hashtable中包含的鍵值對個數。

IsFixedSize:獲取一個值,表示Hashtable是否具有固定大小。

IsReadOnly:獲取一個值,表示Hashtable是否只讀。

Item:獲取或設置與指定的鍵相關的值。

Keys:獲取一個 ICollection,包含Hashtable中的鍵。

Values:獲取一個ICollection,包含Hashtable中的值。

  • public virtual void Add(object key, object value); 向 Hashtable 添加一個帶有指定的鍵和值的元素。
  • public virtual void Clear(); 從 Hashtable 中移除所有的元素。
  • public virtual bool ContainsKey(object key); 判斷 Hashtable 是否包含指定的鍵。
  • public virtual bool ContainsValue(object value); 判斷 Hashtable 是否包含指定的值。
  • public virtual void Remove(object key); 從 Hashtable 中移除帶有指定的鍵的元素。
排序列表sortlist

可以使用鍵或值來訪問列表中的項。

Capacity:獲取或設置SortedList的容量。

Count:獲取 SortedList 中的元素個數。

IsFixedSize:獲取一個值,表示 SortedList 是否具有固定大小。

IsReadOnly:獲取一個值,表示 SortedList 是否只讀。

Item:獲取或設置與 SortedList 中指定的鍵相關的值。

Keys:獲取 SortedList 中的鍵。

Values:獲取 SortedList 中的值。

  • public virtual void Add(object key, object value); 向 SortedList 添加一個帶有指定的鍵和值的元素。
  • public virtual void Clear(); 從 SortedList 中移除所有的元素。
  • public virtual bool ContainsKey(object key); 判斷 SortedList 是否包含指定的鍵。
  • public virtual bool ContainsValue(object value); 判斷 SortedList 是否包含指定的值。
  • public virtual object GetByIndex(int index); 獲取 SortedList 的指定索引處的值。
  • public virtual object GetKey(int index); 獲取 SortedList 的指定索引處的鍵。
  • public virtual IList GetKeyList(); 獲取 SortedList 中的鍵。
  • public virtual IList GetValueList(); 獲取 SortedList 中的值。
  • public virtual int IndexOfKey(object key); 返回 SortedList 中的指定鍵的索引,索引從零開始。
  • public virtual int IndexOfValue(object value); 返回 SortedList 中的指定值第一次出現的索引,索引從零開始。
  • public virtual void Remove(object key); 從 SortedList 中移除帶有指定的鍵的元素。
  • public virtual void RemoveAt(int index); 移除 SortedList 的指定索引處的元素。
  • public virtual void TrimToSize(); 設置容量為 SortedList 中元素的實際個數。
堆棧stack

后進先出隊列。Count:獲取堆棧中的元素個數。

  • public virtual void Clear(); 從 Stack 中移除所有的元素。
  • public virtual bool Contains( object obj ); 判斷某個元素是否在 Stack 中。
  • public virtual object Peek();返回在 Stack 的頂部的對象,但不移除它。
  • public virtual object Pop();移除並返回在 Stack 的頂部的對象。
  • public virtual void Push( object obj );向 Stack 的頂部添加一個對象。
  • public virtual object[] ToArray();復制 Stack 到一個新的數組中。
隊列queue

先進先出隊列。Count:獲取隊列中的元素個數。

  • public virtual void Clear(); 從 Queue 中移除所有的元素。
  • public virtual bool Contains(object obj); 判斷某個元素是否在 Queue 中。
  • public virtual object Dequeue();移除並返回在 Queue 的開頭的對象。
  • public virtual void Enqueue( object obj ); 向 Queue 的末尾添加一個對象。
  • public virtual object[] ToArray();復制 Queue 到一個新的數組中。
  • public virtual void TrimToSize();設置容量為 Queue 中元素的實際個數。
 點陣列BitArray

 代表了使用1和0來表示的二進制數組。

Count:獲取 BitArray 中包含的元素個數。

IsReadOnly:獲取一個值,表示 BitArray 是否只讀。

Item:獲取或設置 BitArray 中指定位置的位的值。

Length:獲取或設置 BitArray 中的元素個數。

  • public BitArray And(BitArray value); 對當前的BitArray中的元素和指定的BitArray中的相對應的元素執行按位與操作。
  • public bool Get(int index); 獲取BitArray中指定位置的位的值。
  • public BitArray Not();把當前的BitArray中的位值反轉,以便設置為true的元素變為false,設置為false的元素變為 true。
  • public BitArray Or(BitArray value); 對當前的BitArray中的元素和指定的BitArray中的相對應的元素執行按位或操作。
  • public void Set(int index, bool value); 把BitArray中指定位置的位設置為指定的值。
  • public void SetAll(bool value); 把BitArray中的所有位設置為指定的值。
  • public BitArray Xor(BitArray value); 對當前的BitArray中的元素和指定的BitArray中的相對應的元素執行按位異或操作。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

簡單實用ArrayList:

 

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

namespace ConsoleApplication3
{
    public class Student
    {
        public int nameNo { get; set; }
        public string Name { get; set; }
        public override string ToString()
        {
            return string.Format("{0}的學號是{1}", this.Name, this.nameNo);
        }
    }
    public class Clazz:IEnumerable
    {
        public ArrayList students = new ArrayList();
        public int AddNewStudent(Student stu)
        {
            return this.students.Add(stu);
        }
        // 實現GetEnumerator接口
        public IEnumerator GetEnumerator()
        {
            foreach(object item in this.students)
            {
                yield return item;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            /*
             * ============================== ArayList ==============================
            // 使用ArrayList創建集合
            ArrayList ab = new ArrayList();  // 創建一個集合
            // 可以添加任何類型的東西
            ab.Add("a");
            ab.Add(123);
            ab.Add(true);
            // object類型
            foreach (object item in ab)
            {
                Console.Write(item);
                Console.Write("\t");
            }
            Console.WriteLine();
            // 可以編譯通過,但是執行會出錯
            *
            foreach(int item in ab)
            {
                // 錯誤原因在於不是整型時,需要進行類型轉換,解決辦法通過類型轉換即可,循環時先使用"object item",然后再"(int)item"進行類型轉換
                // 不過如果進行了類型轉換就涉及到了一個新的問題,那就是裝箱和拆箱的效率問題
                Console.Write(item);
                Console.Write("\t");
            }
            Console.WriteLine();*
            // 計算個數
            Console.WriteLine(ab.Count);
            // 當然也可以通過下標進行訪問
            for (int i = 0; i < ab.Count; i++)
            {
                Console.Write(ab[i]);
                Console.Write("\t");
            }
            Console.ReadKey();
            */
            //////////////////////////////////////////////////
            /*
            // arraylist的另一種用法,對類進行操作
            ArrayList al = new ArrayList();
            al.Add(new Student() { nameNo = 1, Name = "小王" });
            al.Add(new Student() { nameNo = 2, Name = "小李" });
            al.Add(new Student() { nameNo = 5, Name = "小龍" });
            foreach(object item in al)
            {
                Console.WriteLine(item);
            }
            Console.Clear();  // 清屏
            System.Collections.Hashtable NoLIst = new Hashtable();  // 創建指定的序號進行排序
            var s1 = new Student() { nameNo = 1, Name = "小也子" };
            var s2 = new Student() { nameNo = 2, Name = "小青子" };
            var s3 = new Student() { nameNo = 3, Name = "小嵐子" };
            var s4 = new Student() { nameNo = 4, Name = "小寶子" };
            // 也可以獲取前面的值
            var s5 = al[2] as Student;
            // 根據nameNo索引進行排序
            NoLIst.Add(s1.nameNo, s1);
            NoLIst.Add(s2.nameNo, s2);
            NoLIst.Add(s3.nameNo, s3);
            NoLIst.Add(s4.nameNo, s4);
            NoLIst.Add(s5.nameNo, s5);
            foreach(object item in NoLIst)
            {
                Console.WriteLine(item);  // 輸出"System.Collections.DictionaryEntry"
            }
            Console.Clear();
            // 因為上面輸出我們知道是"System.Collections.DictionaryEntry"類型
            foreach(DictionaryEntry item in NoLIst)
            {
                Console.WriteLine("key:{0},value:{1}", item.Key, item.Value);  // 輸出
            }
            Console.Clear();
            Student s = NoLIst[2] as Student;  // 弱類型
            Console.WriteLine("no:{0}, value:{1}", s.nameNo, s.Name);*/
            //
            // ==================================== IEnumerable =================================
            /*
            ArrayList al = new ArrayList();
            al.Add(new Student() { nameNo = 1, Name = "小王" });
            al.Add(new Student() { nameNo = 2, Name = "小李" });
            al.Add(new Student() { nameNo = 5, Name = "小龍" });
            IEnumerable enumer = al;
            IEnumerator iet = enumer.GetEnumerator();  // 返回的是True或者false
            // 往下一個元素移動
            while (iet.MoveNext())
            {
                object currentItem = iet.Current;  // 獲取當前元素
                Console.WriteLine(currentItem);  // 輸出當前元素
            }*/
            var s1 = new Student() { nameNo = 1, Name = "小也子" };
            var s2 = new Student() { nameNo = 2, Name = "小青子" };
            var s3 = new Student() { nameNo = 3, Name = "小嵐子" };
            var s4 = new Student() { nameNo = 4, Name = "小寶子" };
            Clazz clazz = new Clazz();
            clazz.AddNewStudent(s1);
            clazz.AddNewStudent(s2);
            clazz.AddNewStudent(s3);
            clazz.AddNewStudent(s4);
            // 1.Calzz沒有繼承IEnumerable
            /*
             * 報錯,因為並沒有包含IEnumerable
            foreach(Student item in clazz)
            {
                //
            }*/
            // 2.Calzz繼承IEnumerable,並實現GetEnumerator
            IEnumerator iet3 = clazz.GetEnumerator();
            while (iet3.MoveNext())
            {
                Console.WriteLine(iet3.Current);
            }
        }
    }
}

 

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
            //============================================= SortList ===============================
            SortedList sl = new SortedList();  // 初始化
            // 添加健值
            sl.Add("1", "bob1");
            sl.Add("2", "bob2");
            sl.Add("3", "bob3");
            sl.Add("4", "bob4");
            sl.Add("5", "bob5");
            // 此外還有 sl.Contains(健)、sl.ContainsKey(健)
            if (sl.ContainsValue("bob4"))
            {
                // 判斷是否包含某些值
                Console.WriteLine("已包含");
            }
            else
            {
                sl.Add("4", "bob4");
            }
            ICollection keys = sl.Keys;  // 獲取健的集合
            ICollection values = sl.Values;  // 獲取所有值
            foreach (string mykey in keys)
            {
                Console.Write(mykey+" ");
            }
            Console.WriteLine();
            foreach (string myvalue in keys)
            {
                Console.Write(myvalue+" ");
            }*/
            //
            /*
            //================================== stack ================================
            Stack sk = new Stack();  // 初始化
            // 進堆
            sk.Push("a1");
            sk.Push("a2");
            sk.Push("a3");
            sk.Push("a4");
            sk.Push("a5");
            Console.WriteLine(sk.Count);  // 獲取堆的個數
            *
            // 遍歷
            foreach (string item in sk)
            {
                Console.Write(item + " ");
            }*
            //
            // Console.WriteLine(sk.Peek());  // 獲取頂部元素,但是不刪除
            //
            // Console.WriteLine(sk.Pop());  // 彈出(並刪除)一個頂部元素
            */
            //
            /*
            //================================ queue ==========================
            Queue que = new Queue();  // 初始化
            que.Enqueue("bob1");
            que.Enqueue("bob2");
            que.Enqueue("bob3");
            que.Enqueue("bob4");
            que.Enqueue("bob5");
            //
            // Console.WriteLine(que.Count);
            //
            // que.Clear();  // 清空隊列
            *
            //遍歷隊列
            foreach (string mystr in que)
            {
                Console.Write(mystr+" ");
            }
            *
            //Console.WriteLine(que.Dequeue());  // 彈出(並刪除)頭部隊列
            */
            //
            //================================ BitArray ===================================
            // 創建兩個大小為8的點陣列
            BitArray ba1 = new BitArray(8);
            BitArray ba2 = new BitArray(8);
            byte[] a = { 60 };
            byte[] b = { 13 };
            // 把60和13,添加到點陣列中
            ba1 = new BitArray(a);
            ba2 = new BitArray(b);
            //Console.WriteLine(ba1.Count);  // 獲取元素數量
            // 獲取ba1的內容
            for (int i = 0; i < ba1.Count; i++)
            {
                Console.Write("{0,-6}", ba1[i]);
            }
            Console.WriteLine();
            // 獲取ba2的內容
            for (int i = 0; i < ba2.Count; i++)
            {
                Console.Write("{0,-6}", ba2[i]);
            }
            Console.WriteLine();
            BitArray ba3 = new BitArray(8);
            /*
            ba3 = ba1.And(ba2);  // 執行and運算
            // 獲取ba3的內容
            for (int i = 0; i < ba3.Count; i++)
            {
                Console.Write("{0,-6}", ba3[i]);
            }
            Console.WriteLine();*/
            ba3 = ba1.Or(ba2);  // 進行or運算
            for (int i = 0; i < ba3.Count; i++)
            {
                Console.Write("{0,-6}", ba3[i]);
            }
        }
    }
}

 


免責聲明!

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



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