zoukankan      html  css  js  c++  java
  • C#.net技术内幕04集合

    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等对元素进行操作。

      

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

       BStack:是一种后进先出结构。有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(
    "************");
            }

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

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

    还有使用枚举数的时候不再是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(
    "************");
            }

    DBitarray:一组真假值的集合。遍历的方法与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(
    "************");
            }

         ESortedlist:这种用法和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();}
            }
        

  • 相关阅读:
    Day 20 初识面向对象
    Day 16 常用模块
    Day 15 正则表达式 re模块
    D14 模块 导入模块 开发目录规范
    Day 13 迭代器,生成器,内置函数
    Day 12 递归,二分算法,推导式,匿名函数
    Day 11 闭包函数.装饰器
    D10 函数(二) 嵌套,命名空间作用域
    D09 函数(一) 返回值,参数
    Day 07 Day08 字符编码与文件处理
  • 原文地址:https://www.cnblogs.com/janes/p/1414679.html
Copyright © 2011-2022 走看看