c# 集合对象详解

来源:互联网 发布:矩阵特征根 编辑:程序博客网 时间:2024/04/30 09:14

1.ArrayList

ArrayList类主要用于对一个数组中的元素进行各种处理。在ArrayList中主要使用Add、Remove、RemoveAt、Insert四个方法对栈进行操作。Add方法用于将对象添加到 ArrayList 的结尾处;Remove方法用于从 ArrayList 中移除特定对象的第一个匹配项;RemoveAt方法用于移除 ArrayList 的指定索引处的元素;Insert方法用于将元素插入 ArrayList 的指定索引处

   1:  using System.Collections;//引入命名空间
   2:  namespace _4
   3:  {
   4:      class ArrayListTest
   5:      {
   6:          static void Main(string[] args)
   7:          {
   8:              ArrayList arrlist = new ArrayList();//实例化一个ArrayList对象
   9:              //使用Add方法向ArrayList中添加元素,将元素添加到ArrayList对象的末尾
  10:              arrlist.Add("苹果");
  11:              arrlist.Add("香蕉");
  12:              arrlist.Add("葡萄");
  13:              foreach (int n in new int[3] { 0, 1, 2 })
  14:              {
  15:                  arrlist.Add(n);
  16:              }
  17:              //移除值为的第一个元素
  18:              arrlist.Remove(0);
  19:              //移除当前索引为的元素,即第个元素
  20:              arrlist.RemoveAt(3);
  21:              //在指定索引处添加一个元素
  22:              arrlist.Insert(1, "apple");
  23:              //遍历ArrayList,并输出所有元素
  24:              for (int i = 0; i < arrlist.Count; i++)
  25:              {
  26:                  Console.WriteLine(arrlist[i].ToString());
  27:              }
  28:          }
  29:      }
  30:  }

2.Stack

Stack(堆栈)类主要实现了一个LIFO(Last In First Out,后进先出)的机制。元素从栈的顶部插入(入栈操作),也从堆的顶部移除(出栈操作)。在Stack中主要使用Push,Pop,Peek三个方法对栈进行操作。Push方法用于将对象插入 Stack 的顶部;Pop方法用于移除并返回位于 Stack 顶部的对象;Peek方法用于返回位于 Stack 顶部的对象但不将其移除。

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Collections;
   6:   
   7:  namespace Stack
   8:  {
   9:      class Program
  10:      {
  11:          static void Main(string[] args)
  12:          {
  13:              Stack<int> stack = new Stack<int>();
  14:   
  15:              //入栈操作, 使用push方法向stack的顶部添加数据
  16:              for (int i = 1; i < 6; i++ )
  17:              {
  18:                  stack.Push(i);
  19:                  Console.WriteLine("{0}入栈", i);
  20:              }
  21:   
  22:              //返回栈顶元素
  23:              Console.WriteLine("当前栈顶元素为:{0}", (stack.Peek()).ToString());
  24:              //出栈
  25:              Console.WriteLine("移除栈顶元素:{0}", stack.Pop().ToString());
  26:              //返回栈顶元素
  27:              Console.WriteLine("当前栈顶元素为:{0}", stack.Peek().ToString());
  28:   
  29:              //遍历栈
  30:              Console.WriteLine("遍历栈");
  31:              foreach (int i in stack)
  32:              {
  33:                  Console.WriteLine(i);
  34:              }
  35:   
  36:              //清空栈
  37:              while (stack.Count != 0)
  38:              {
  39:                  int s = (int)stack.Pop();
  40:                  Console.WriteLine("{0}出栈", s);
  41:              }
  42:          }
  43:      }
  44:  }

3.Queue

Queue(队列)类主要实现了一个FIFO(First In First Out,先进先出)的机制。元素在队列的尾部插入(入队操作),并从队列的头部移出(出队操作)。在Queue中主要使用Enqueue、Dequeue、Peek三个方法对队进行操作。Enqueue方法用于将对象添加到 Queue 的结尾处;Dequeue方法移除并返回位于 Queue 开始处的对象;Peek方法用于返回位于 Queue 开始处的对象但不将其移除。

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Collections;
   6:   
   7:  namespace Queue
   8:  {
   9:      class Program
  10:      {
  11:          static void Main(string[] args)
  12:          {
  13:              //实例化Queue类的对象
  14:              Queue<int> queue = new Queue<int>();
  15:   
  16:              //人队列,使用enqueue方法向Queue队列中添加元素
  17:              for (int i=1; i<6; i++)
  18:              {
  19:                  queue.Enqueue(i);
  20:                  Console.WriteLine("{0}入队", i);
  21:              }
  22:   
  23:              //返回队列开始处的元素
  24:              Console.WriteLine("当前队列开始处的元素为:{0}", queue.Peek().ToString());
  25:              //遍历队
  26:              Console.WriteLine("遍历队");
  27:              foreach (int i in queue)
  28:              {
  29:                  Console.WriteLine(i);
  30:              }
  31:   
  32:              //清空队列
  33:              while (queue.Count != 0)
  34:              {
  35:                  int q = queue.Dequeue();
  36:                  Console.WriteLine("{0}出队", q);
  37:              }
  38:          }
  39:      }
  40:  }

4.Hashtable

Hashtable(哈希表)是一种键/值对集合,这些键/值对根据键的哈希代码进行组织。在一个Hashtable中插入一对Key/Value时,它自动将Key值映射到Value,并允许获取与一个指定的Key相关联的value。在Hashtable中主要使用Add、Remove两个方法对哈希表进行操作。Add方法用于将带有指定键和值的元素添加到 Hashtable 中;Remove方法用于从 Hashtable 中移除带有指定键的元素。

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Collections;
   6:   
   7:  namespace HashTable
   8:  {
   9:      class Program
  10:      {
  11:          static void Main(string[] args)
  12:          {
  13:              //实例化HashTable类的对象
  14:              Hashtable student = new Hashtable();
  15:   
  16:              //向HashTable中添加元素
  17:              for (int i = 0; i < 10; i++ )
  18:              {
  19:                  string key = "s100" + i.ToString();
  20:                  string value = "kof" + i.ToString();
  21:   
  22:                  //先判断是否有已经存在的键
  23:                  if (!student.ContainsKey(key))
  24:                  {
  25:                      student.Add(key, value);
  26:                  }
  27:              }
  28:   
  29:              //遍历HashTable
  30:              foreach (DictionaryEntry element in student)
  31:              {
  32:                  //获取或设置键值对中的键
  33:                  string id = element.Key.ToString();
  34:                  //获取键值对中的值
  35:                  string name = element.Value.ToString();
  36:                  Console.WriteLine("学生的ID:{0} 学生姓名:{1}", id, name);
  37:              }
  38:   
  39:              //移除HashTable中的元素,根据键
  40:              student.Remove("s1003");
  41:              Console.WriteLine("已经移除后的哈希表:");
  42:   
  43:              //DictionaryEntry 键值对对象
  44:              foreach (DictionaryEntry element in student)
  45:              {
  46:                  //获取或设置键值对中的键
  47:                  string id = element.Key.ToString();
  48:                  //获取键值对中的值
  49:                  string name = element.Value.ToString();
  50:                  Console.WriteLine("学生的ID:{0} 学生姓名:{1}", id, name);
  51:              }
  52:          }
  53:      }
  54:  }

5,Dictionary<,>

Dictionary是一个泛型

他本身有集合的功能有时候可以把它看成数组

他的结构是这样的:Dictionary<[key], [value]>

他的特点是存入对象是需要与[key]值一一对应的存入该泛型

   1:  Dictionary<int, string> dic = new Dictionary<int, string>();
   2:              
   3:              //添加键值对
   4:              dic.Add(1, "one");
   5:              dic.Add(2, "two");
   6:              dic.Add(3, "three");
   7:              //提取元素的方法
   8:              for (int i = 1; i <= 3; i++ )
   9:              {
  10:                  Console.WriteLine("{0}个元素是{1}", i, dic[i]);
  11:              }