C#.net技术内幕04-集合

本文详细介绍了C#中常用的集合类型,包括锯齿数组、ArrayList、Stack、Hashtable、BitArray、SortedList和Queue等。每种集合都通过示例代码展示了基本的使用方法,如添加、删除元素以及使用枚举数进行遍历。
  1.  锯齿数组:锯齿数组就是数组的数组。例如:

 

Int[] a=new int[][]{
         New 
int[]{111};
          New 
int[]{2312};
}

    2.Out与ref:将数组作为参数传递时,在调用函数之前不需要初始化out参数,但调用的函数必须在返回前分配数组类型。而且,ref参数必须在函数调用之前分配。

  

    3. 枚举数(enumerator):

    它是一个对象,可以通过它对集合项进行便利。枚举数只能读取但不能改变集合的至。在实例化后,会被放在集合第一个元素之前。如果不调用movenexe就直接使用current访问的话会出错。当枚举数到达集合的末尾时,会停留在集合的最后一个元素后面,并返回假。如果此时继续使用current也会出错。

    主要方法属性:

  • current返回集合中的当前对象;
  • Movenext将枚举项后移一项;
  • Reset将枚举数移到初始位置。

    4.几种常用的集合:

   AArraylisy:使用add,remove等对元素进行操作。

  

ExpandedBlockStart.gifView Code
public static void arraylist1()//arraylist的使用
        {
            ArrayList arr 
= new ArrayList();
            
for (int i = 1; i <=5; i++)
                arr.Add(i.ToString());
            enumerator(arr);
            arr.Remove(arr[
6]);
            enumerator(arr);
        }

public static void enumerator()//使用枚举数遍历arraylist集合
        {
            ArrayList arr 
= new ArrayList();
            arr.Add(
"hello");
            arr.Add(
"world");
            arr.Add(
"peace");
            IEnumerator en 
= arr.GetEnumerator();
            
while (en.MoveNext())
                Console.WriteLine(en.Current);
            Console.ReadLine();
        }

 

   BStack:是一种后进先出结构。有push。Pop。peek三种方法。

 

ExpandedBlockStart.gifView Code
protected static void stack()//stack的使用

        {
            Stack stk 
= new Stack();
            
for (int i = 1; i <= 5; i++)
            stk.Push(i.ToString());
            enumerator(stk );
            stk.Pop();
            enumerator(stk );
        }

protected static void enumerator(Stack arr)//使用枚举数遍历stack集合
        {
            IEnumerator en 
= arr.GetEnumerator();
            
while (en.MoveNext())
                Console.WriteLine(en.Current);
            Console.WriteLine(
"************");
        }

 

CHashtable:这是一种键值对集合。该集合与前面的两种集合不太一样,我写了段简单代码试了一下,发现了一下几点:

首先他得到的并不是原来输入的顺序,而是进行散列后的值;

还有使用枚举数的时候不再是Ienumerator,而是IdictionaryEnumerator;

最后,在使用枚举数读取值的时候不再是en.current,而是en.value。

 

ExpandedBlockStart.gifView Code
protected static void hashtable()//hashtable的使用

        {
            Hashtable ht 
= new Hashtable();
            
for (int i = 1; i <= 5; i++)
                ht.Add( 
"num"+i.ToString (),i.ToString());
            enumerator(ht);
            ht.Remove(
"num3");
            enumerator(ht);
        }

        
protected static void enumerator(Hashtable arr)//使用枚举数遍历hashtable集合
        {
            IDictionaryEnumerator en 
= arr.GetEnumerator();
            
while (en.MoveNext())
            Console.Write(en.Value );
            Console.WriteLine(
"************");
        }

 

DBitarray:一组真假值的集合。遍历的方法与arraylist以及stack相同。

要注意以下几点:

首先bitarray实例化时必须声明其长度;

其次,为其添加元素时用的方法是set;

最后,遍历时使用的枚举数仍未Ienumerator。

 

ExpandedBlockStart.gifView Code
protected static void bitarray()//bitarray的使用
        {
            BitArray ba 
= new BitArray(5);
            
for (int i = 0; i <= 4; i++)
            ba.Set(i, i 
% 2 == 0);
            enumerator(ba);
        }

        
protected static void enumerator(BitArray arr)//使用枚举数遍历bitarray集合
        {
            IEnumerator en 
= arr.GetEnumerator();
            
while (en.MoveNext())
            Console.Write(en.Current );
            Console.WriteLine(
"************");
        }

     ESortedlist:这种用法和hashtable与arraylist有点像。

首先,添加元素用的是ADD方法,添加的是键值对;

其次,遍历时使用的枚举数和Hahstable比较像。读取的时候既可以根据键值读取,也可以根据索引值读取。

 

ExpandedBlockStart.gifView Code
 protected static void sortedlist()
        {
            SortedList sl 
= new SortedList();
            
for (int i = 0; i < 5; i++)
                sl.Add(
"num" + i.ToString(), i.ToString());
            enumerator(sl);
        }

        
protected static void enumerator(SortedList arr)//使用枚举数遍历hashtable集合
        {
            IDictionaryEnumerator en 
= arr.GetEnumerator();
            
while (en.MoveNext())
            Console.Write(en.Value);
            Console.WriteLine(arr[
"num2"]);
            Console.WriteLine(arr.GetByIndex(
2));
            Console.WriteLine(
"************");
        }

 

   F:queue这是一种先进先出队列结构。入队方法为enqueque ,出队方法为dequeque。枚举数遍历方式使用Ienumerator,

 

ExpandedBlockStart.gifView Code
protected static void queue()

        {
            Queue q 
= new Queue();
            
for (int i = 0; i < 5; i++)
            q.Enqueue(i.ToString());
            enumerator(q );
            q.Dequeue();
            enumerator(q);
        }

        
protected static void enumerator(Queue q)//使用枚举数遍历queue集合
        {
            IEnumerator en 
= q.GetEnumerator();
            
while (en.MoveNext()){
            Console.Write(en.Current);
            Console.WriteLine();}
        }
    

转载于:https://www.cnblogs.com/janes/archive/2009/03/17/1414679.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值