跳过正文
  1. 文章/
  2. C#/

12、集合

·2439 字·5 分钟· loading · loading · ·
C#
GradyYoung
作者
GradyYoung
C# - 点击查看当前系列文章
§ 12、集合 「 当前文章 」
描述和用法
动态数组(ArrayList) 它代表了可被单独索引的对象的有序集合。它基本上可以替代一个数组。但是,与数组不同的是,您可以使用索引在指定的位置添加和移除项目,动态数组会自动重新调整它的大小。它也允许在列表中进行动态内存分配、增加、搜索、排序各项。
哈希表(Hashtable) 它使用来访问集合中的元素。当您使用键访问元素时,则使用哈希表,而且您可以识别一个有用的键值。哈希表中的每一项都有一个键/值对。键用于访问集合中的项目。
排序列表(SortedList) 它可以使用索引来访问列表中的项。排序列表是数组和哈希表的组合。它包含一个可使用键或索引访问各项的列表。如果您使用索引访问各项,则它是一个动态数组(ArrayList),如果您使用键访问各项,则它是一个哈希表(Hashtable)。集合中的各项总是按键值排序。
堆栈(Stack) 它代表了一个后进先出的对象集合。当您需要对各项进行后进先出的访问时,则使用堆栈。当您在列表中添加一项,称为推入元素,当您从列表中移除一项时,称为弹出元素。
队列(Queue) 它代表了一个先进先出的对象集合。当您需要对各项进行先进先出的访问时,则使用队列。当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队
点阵列(BitArray) 它代表了一个使用值 1 和 0 来表示的二进制数组。当您需要存储位,但是事先不知道位数时,则使用点阵列。您可以使用整型索引从点阵列集合中访问各项,索引从零开始。

ArrayList
#

using System;
using System.Collections;

namespace CollectionApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();

            Console.WriteLine("Adding some numbers:");
            al.Add(45);
            al.Add(78);
            al.Add(33);
            al.Add(56);
            al.Add(12);
            al.Add(23);
            al.Add(9);
            
            Console.WriteLine("Capacity: {0} ", al.Capacity);
            Console.WriteLine("Count: {0}", al.Count);
                      
            Console.Write("Content: ");
            foreach (int i in al)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            Console.Write("Sorted Content: ");
            al.Sort();
            foreach (int i in al)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}

属性
#

属性 描述
Capacity 获取或设置 ArrayList 可以包含的元素个数。
Count 获取 ArrayList 中实际包含的元素个数。
IsFixedSize 获取一个值,表示 ArrayList 是否具有固定大小。
IsReadOnly 获取一个值,表示 ArrayList 是否只读。
IsSynchronized 获取一个值,表示访问 ArrayList 是否同步(线程安全)。
Item[Int32] 获取或设置指定索引处的元素。
SyncRoot 获取一个对象用于同步访问 ArrayList。

方法
#

序号 方法名 & 描述
1 public virtual int Add( object value ); 在 ArrayList 的末尾添加一个对象。
2 public virtual void AddRange( ICollection c ); 在 ArrayList 的末尾添加 ICollection 的元素。
3 public virtual void Clear(); 从 ArrayList 中移除所有的元素。
4 public virtual bool Contains( object item ); 判断某个元素是否在 ArrayList 中。
5 public virtual ArrayList GetRange( int index, int count ); 返回一个 ArrayList,表示源 ArrayList 中元素的子集。
6 public virtual int IndexOf(object); 返回某个值在 ArrayList 中第一次出现的索引,索引从零开始。
7 public virtual void Insert( int index, object value ); 在 ArrayList 的指定索引处,插入一个元素。
8 public virtual void InsertRange( int index, ICollection c ); 在 ArrayList 的指定索引处,插入某个集合的元素。
9 public virtual void Remove( object obj ); 从 ArrayList 中移除第一次出现的指定对象。
10 public virtual void RemoveAt( int index ); 移除 ArrayList 的指定索引处的元素。
11 public virtual void RemoveRange( int index, int count ); 从 ArrayList 中移除某个范围的元素。
12 public virtual void Reverse(); 逆转 ArrayList 中元素的顺序。
13 public virtual void SetRange( int index, ICollection c ); 复制某个集合的元素到 ArrayList 中某个范围的元素上。
14 public virtual void Sort(); 对 ArrayList 中的元素进行排序。
15 public virtual void TrimToSize(); 设置容量为 ArrayList 中元素的实际个数。

Hashtable
#

using System;
using System.Collections;

namespace CollectionsApplication
{
   class Program
   {
      static void Main(string[] args)
      {
         Hashtable ht = new Hashtable();


         ht.Add("001", "Zara Ali");
         ht.Add("002", "Abida Rehman");
         ht.Add("003", "Joe Holzner");
         ht.Add("004", "Mausam Benazir Nur");
         ht.Add("005", "M. Amlan");
         ht.Add("006", "M. Arif");
         ht.Add("007", "Ritesh Saikia");

         if (ht.ContainsValue("Nuha Ali"))
         {
            Console.WriteLine("This student name is already in the list");
         }
         else
         {
            ht.Add("008", "Nuha Ali");
         }
         // 获取键的集合 
         ICollection key = ht.Keys;

         foreach (string k in key)
         {
            Console.WriteLine(k + ": " + ht[k]);
         }
         Console.ReadKey();
      }
   }
}

属性
#

属性 描述
Count 获取 Hashtable 中包含的键值对个数。
IsFixedSize 获取一个值,表示 Hashtable 是否具有固定大小。
IsReadOnly 获取一个值,表示 Hashtable 是否只读。
Item 获取或设置与指定的键相关的值。
Keys 获取一个 ICollection,包含 Hashtable 中的键。
Values 获取一个 ICollection,包含 Hashtable 中的值。

方法
#

序号 方法名 & 描述
1 public virtual void Add( object key, object value ); 向 Hashtable 添加一个带有指定的键和值的元素。
2 public virtual void Clear(); 从 Hashtable 中移除所有的元素。
3 public virtual bool ContainsKey( object key ); 判断 Hashtable 是否包含指定的键。
4 public virtual bool ContainsValue( object value ); 判断 Hashtable 是否包含指定的值。
5 public virtual void Remove( object key ); 从 Hashtable 中移除带有指定的键的元素。

SortedList
#

using System;
using System.Collections;

namespace CollectionsApplication
{
   class Program
   {
      static void Main(string[] args)
      {
         SortedList sl = new SortedList();

         sl.Add("001", "Zara Ali");
         sl.Add("002", "Abida Rehman");
         sl.Add("003", "Joe Holzner");
         sl.Add("004", "Mausam Benazir Nur");
         sl.Add("005", "M. Amlan");
         sl.Add("006", "M. Arif");
         sl.Add("007", "Ritesh Saikia");

         if (sl.ContainsValue("Nuha Ali"))
         {
            Console.WriteLine("This student name is already in the list");
         }
         else
         {
            sl.Add("008", "Nuha Ali");
         }

         // 获取键的集合 
         ICollection key = sl.Keys;

         foreach (string k in key)
         {
            Console.WriteLine(k + ": " + sl[k]);
         }
      }
   }
}

属性
#

属性 描述
Capacity 获取或设置 SortedList 的容量。
Count 获取 SortedList 中的元素个数。
IsFixedSize 获取一个值,表示 SortedList 是否具有固定大小。
IsReadOnly 获取一个值,表示 SortedList 是否只读。
Item 获取或设置与 SortedList 中指定的键相关的值。
Keys 获取 SortedList 中的键。
Values 获取 SortedList 中的值。

方法
#

序号 方法名 & 描述
1 public virtual void Add( object key, object value ); 向 SortedList 添加一个带有指定的键和值的元素。
2 public virtual void Clear(); 从 SortedList 中移除所有的元素。
3 public virtual bool ContainsKey( object key ); 判断 SortedList 是否包含指定的键。
4 public virtual bool ContainsValue( object value ); 判断 SortedList 是否包含指定的值。
5 public virtual object GetByIndex( int index ); 获取 SortedList 的指定索引处的值。
6 public virtual object GetKey( int index ); 获取 SortedList 的指定索引处的键。
7 public virtual IList GetKeyList(); 获取 SortedList 中的键。
8 public virtual IList GetValueList(); 获取 SortedList 中的值。
9 public virtual int IndexOfKey( object key ); 返回 SortedList 中的指定键的索引,索引从零开始。
10 public virtual int IndexOfValue( object value ); 返回 SortedList 中的指定值第一次出现的索引,索引从零开始。
11 public virtual void Remove( object key ); 从 SortedList 中移除带有指定的键的元素。
12 public virtual void RemoveAt( int index ); 移除 SortedList 的指定索引处的元素。
13 public virtual void TrimToSize(); 设置容量为 SortedList 中元素的实际个数。

Stack
#

using System;
using System.Collections;

namespace CollectionsApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack st = new Stack();

            st.Push('A');
            st.Push('M');
            st.Push('G');
            st.Push('W');
            
            Console.WriteLine("Current stack: ");
            foreach (char c in st)
            {
                Console.Write(c + " ");
            }
            Console.WriteLine();
            
            st.Push('V');
            st.Push('H');
            Console.WriteLine("The next poppable value in stack: {0}", 
            st.Peek());
            Console.WriteLine("Current stack: ");           
            foreach (char c in st)
            {
               Console.Write(c + " ");
            }
            Console.WriteLine();

            Console.WriteLine("Removing values ");
            st.Pop();
            st.Pop();
            st.Pop();
            
            Console.WriteLine("Current stack: ");
            foreach (char c in st)
            {
               Console.Write(c + " "); 
            }
        }
    }
}

属性
#

属性 描述
Count 获取 Stack 中包含的元素个数。

方法
#

序号 方法名 & 描述
1 public virtual void Clear(); 从 Stack 中移除所有的元素。
2 public virtual bool Contains( object obj ); 判断某个元素是否在 Stack 中。
3 public virtual object Peek(); 返回在 Stack 的顶部的对象,但不移除它。
4 public virtual object Pop(); 移除并返回在 Stack 的顶部的对象。
5 public virtual void Push( object obj ); 向 Stack 的顶部添加一个对象。
6 public virtual object[] ToArray(); 复制 Stack 到一个新的数组中。

Queue
#

using System;
using System.Collections;

namespace CollectionsApplication
{
   class Program
   {
      static void Main(string[] args)
      {
         Queue q = new Queue();

         q.Enqueue('A');
         q.Enqueue('M');
         q.Enqueue('G');
         q.Enqueue('W');
         
         Console.WriteLine("Current queue: ");
         foreach (char c in q)
            Console.Write(c + " ");
         Console.WriteLine();
         q.Enqueue('V');
         q.Enqueue('H');
         Console.WriteLine("Current queue: ");         
         foreach (char c in q)
            Console.Write(c + " ");
         Console.WriteLine();
         Console.WriteLine("Removing some values ");
         char ch = (char)q.Dequeue();
         Console.WriteLine("The removed value: {0}", ch);
         ch = (char)q.Dequeue();
         Console.WriteLine("The removed value: {0}", ch);
         Console.ReadKey();
      }
   }
}

属性
#

属性 描述
Count 获取 Queue 中包含的元素个数。

方法
#

序号 方法名 & 描述
1 public virtual void Clear(); 从 Queue 中移除所有的元素。
2 public virtual bool Contains( object obj ); 判断某个元素是否在 Queue 中。
3 public virtual object Dequeue(); 移除并返回在 Queue 的开头的对象。
4 public virtual void Enqueue( object obj ); 向 Queue 的末尾添加一个对象。
5 public virtual object[] ToArray(); 复制 Queue 到一个新的数组中。
6 public virtual void TrimToSize(); 设置容量为 Queue 中元素的实际个数。
C# - 点击查看当前系列文章
§ 12、集合 「 当前文章 」