zoukankan      html  css  js  c++  java
  • C#基本语法复习-使用数组和集合

    什么是数组?

      数组是一个无序的元素序列,数组中的所有元素都具有相同的类型。

    声明数组变量:

      int[] a;数组的大小不是声明的一部分;

    创建数组的实例:

      a=new int[3];

    初始化数组变量:

      int[] a=new int[2]{1,2};

      int[] a={1,2};

      Time[] schedule={new Time(1,2),new Time(5,30)};

    创建隐式类型的数组:

      var name=new[]{new {Name="John",Age=44},new {Name="Diana",Age=45}};

    遍历数组:

      可以使用for语句,或者是foreach语句,在不知道数组的类型时可以将类型定义为var;

    复制数组:

      int pins={9,3,7,2};

      int[] copy=new int[pins.Length];

      pins.copyto(copy,0);

      使用system.array的copy(源,拷贝到的数组,数组的长度);

    使用多维数组:

      int[,] items=new int[4,6];

    什么事集合类:

      集合类中的元素是object类型的,在存储数值时会对数值直接进行装箱;

    arraylist集合类:

      有add(),remove()insert()方法;

    queue集合类:

      有enqueque与dequeue两种方法;

    stack集合类:

      有push与pop两种类型;

    hashtable集合类:

      hashtable a =new hashtable();

      a["z"]=23;

      a["w"]=24;

      froeach(DictionaryEntry element in ages)

      {

        string name=(string)element.key;

        int age=(int)element.value;//拆箱进行强制类型转化

      }

    sortedList与hashtable使用方法相同;

  • 相关阅读:
    sql 中 in 与 exist 的区别
    with as (cte common table expression) 公共表表达式
    配置连接数据库的方式
    Dispose 与 close 方法 的区别
    抽象类
    default
    什么叫无符号整型
    hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]
    poj 2480 Longge's problem [ 欧拉函数 ]
    lightoj 1293
  • 原文地址:https://www.cnblogs.com/hanshuidecangsanggan/p/4388088.html
Copyright © 2011-2022 走看看