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使用方法相同;

  • 相关阅读:
    网络编程-python实现-UDP(1.1.2)
    网络编程-python实现-socket(1.1.1)
    1-浮动布局
    1-解决java Scanner出现 java.util.NoSuchElementException
    1.激活函数
    A-交叉熵的使用
    matplotlib的学习16-animation动画
    matplotlib的学习15-次坐标轴
    matplotlib的学习14-图中图
    07-爬虫验证码破解实战
  • 原文地址:https://www.cnblogs.com/hanshuidecangsanggan/p/4388088.html
Copyright © 2011-2022 走看看