C#的6种常用集合类大比拼

本文详细介绍了C#中的几种常用集合类,包括ArrayList、Stack、Queue、Hashtable和SortedList等,解释了每种集合的特点及使用方法,并通过示例代码展示了如何进行添加、删除、遍历等操作。

ArrayList类

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
... {
    
class Program
    
...{
        
static void Main(string[] args)
        
...{
             ArrayList al
= new ArrayList();
             al.Add(
100);//单个添加
            foreach (int number in new int[6] ...{ 9, 3, 7, 2, 4, 8 })
            
...{
                 al.Add(number);
//集体添加方法一//清清月儿 http://blog.youkuaiyun.com/21aspnet/
             }
            
int[] number2 = new int[2] ...{ 11,12 };
             al.AddRange(number2);
//集体添加方法二
             al.Remove(3);//移除值为3的
             al.RemoveAt(3);//移除第3个
             ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取旧ArrayList一部份


             Console.WriteLine(
"遍历方法一:");
            
foreach (int i in al)//不要强制转换
            ...{
                 Console.WriteLine(i);
//遍历方法一
             }

             Console.WriteLine(
"遍历方法二:");
            
for (int i = 0; i != al2.Count; i++)//数组是length
            ...{
                
int number = (int)al2[i];//一定要强制转换
                 Console.WriteLine(number);//遍历方法二

             }

         }

     }

}

 

 

 

 

 

 

 

 

 

 

 

 

2.Stack类

栈,后进先出。push方法入栈,pop方法出栈。

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
... {
    
class Program
    
...{
        
static void Main(string[] args)
        
...{
              Stack sk
= new Stack();
              Stack sk2
= new Stack();
            
foreach (int i in new int[4] ...{ 1, 2, 3, 4 })
            
...{
                  sk.Push(i);
//填充
                  sk2.Push(i);
              }

            
            
foreach (int i in sk)
            
...{
                  Console.WriteLine(i);
//遍历
              }

              sk.Pop();
              Console.WriteLine(
"Pop");
            
foreach (int i in sk)
            
...{
                  Console.WriteLine(i);
              }

            
              sk2.Peek();
//弹出最后一项不删除//清清月儿 http://blog.youkuaiyun.com/21aspnet/
              Console.WriteLine("Peek");
            
foreach (int i in sk2)
            
...{
                  Console.WriteLine(i);
              }


            
while (sk2.Count != 0)
            
...{
                
int i = (int)sk2.Pop();//清空
                  sk2.Pop();//清空
              }
              Console.WriteLine(
"清空");
            
foreach (int i in sk2)
            
...{
                  Console.WriteLine(i);
              }

          }

      }

}

 

 

 

 

 

 

 

 

 

3.Queue类

队列,先进先出。enqueue方法入队列,dequeue方法出队列。

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
... {
    
class Program
    
...{
        
static void Main(string[] args)
        
...{
              Queue qu
= new Queue();
              Queue qu2
= new Queue();
            
foreach (int i in new int[4] ...{ 1, 2, 3, 4 })
            
...{
                  qu.Enqueue(i);
//填充
                  qu2.Enqueue(i);
              }

            
            
foreach (int i in qu)
            
...{
                  Console.WriteLine(i);
//遍历
              }

              qu.Dequeue();
              Console.WriteLine(
"Dequeue");
            
foreach (int i in qu)
            
...{
                  Console.WriteLine(i);
              }

            
              qu2.Peek();
//弹出最后一项不删除
              Console.WriteLine("Peek");
            
foreach (int i in qu2)
            
...{
                  Console.WriteLine(i);
              }


            
while (qu2.Count != 0)
            
...{
                
int i = (int)qu2.Dequeue();//清空
                  qu2.Dequeue();//清空
              }
              Console.WriteLine(
"清空");
            
foreach (int i in qu2)
            
...{
                  Console.WriteLine(i);
              }

          }

      }

}

 

 

 

 

 

 

4.Hashtable类

哈希表,名-值对。类似于字典(比数组更强大)。哈希表是经过优化的,访问下标的对象先散列过。如果以任意类型键值访问其中元素会快于其他集合。GetHashCode()方法返回一个int型数据,使用这个键的值生成该int型数据。哈希表获取这个值最后返回一个索引,表示带有给定散列的数据项在字典中存储的位置。

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
... {
    
class Program
    
...{
        
public static void Main()
        
...{

            
// Creates and initializes a new Hashtable.
              Hashtable myHT = new Hashtable();
              myHT.Add(
"one", "The");
              myHT.Add(
"two", "quick");
              myHT.Add(
"three", "brown");
              myHT.Add(
"four", "fox");

            
// Displays the Hashtable.//清清月儿 http://blog.youkuaiyun.com/21aspnet/
              Console.WriteLine("The Hashtable contains the following:");
              PrintKeysAndValues(myHT);
          }



        
public static void PrintKeysAndValues(Hashtable myHT)
        
...{
            
foreach (string s in myHT.Keys)
                  Console.WriteLine(s);

              Console.WriteLine(
" -KEY- -VALUE-");
            
foreach (DictionaryEntry de in myHT)
                  Console.WriteLine(
" {0}: {1}", de.Key, de.Value);
              Console.WriteLine();
          }

      }

}

 

 

 

 

 

5.SortedList类

与哈希表类似,区别在于SortedList中的Key数组排好序的。

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
... {
    
class Program
    
...{
        
public static void Main()
        
...{

              SortedList sl
= new SortedList();
              sl[
"c"] = 41;
              sl[
"a"] = 42;
              sl[
"d"] = 11;
              sl[
"b"] = 13;

            
foreach (DictionaryEntry element in sl)
            
...{
                
string s = (string)element.Key;
                
int i = (int)element.Value;
                  Console.WriteLine(
"{0},{1}",s,i);
              }

          }

      }

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值