• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
敬YES
Now Or Never
博客园    首页       联系   管理    订阅  订阅
C#.net技术内幕04-集合
  1.  锯齿数组:锯齿数组就是数组的数组。例如:

 

Int[] a=new int[][]{
         New 
int[]{1,1,1};
          New 
int[]{23,12};
}

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

  

    3. 枚举数(enumerator):

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

    主要方法属性:

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

    4.几种常用的集合:

   A.Arraylisy:使用add,remove等对元素进行操作。

  

View 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();
        }

 

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

 

View 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(
"************");
        }

 

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

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

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

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

 

View 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(
"************");
        }

 

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

要注意以下几点:

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

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

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

 

View 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(
"************");
        }

     E.Sortedlist:这种用法和hashtable与arraylist有点像。

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

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

 

View 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,

 

View 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();}
        }
    

作者:陈敬(公众号:敬YES)
出处:http://www.cnblogs.com/janes/
博客文章仅供交流学习,请勿用于商业用途。如需转载,请务必注明出处。

posted on 2009-03-17 20:22  敬YES  阅读(926)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3