zoukankan      html  css  js  c++  java
  • C#数组

    一、声明数级时要在各个元素的类型后面加上一组方括号,例:

    int[] intergers;

    二、要初始化特定大小的数组,可以使用new关键字,在类型后面的方括号中给出大小。例:

    int[] integers=new int[2];  

    注意:在c语言中声明数组并分配内存空间只需int a[3]即可。而在c#中声明变量int[] integers;,分配内存空间new int[2];

    此外,中括号的位置也于c语言不同。c语言中中括号在变量的后面,而c#中中括号在类型的后面。例:

    c语言:int a[2];

    c#:int[2] a;

    三、也可以声明时不初始化,以后动态指定大小。例:

    int[] integers;

    integers=new int[32];

    四、在初始化数组时,不能用变量设置数组应包含多少元素,但是可以给数组长度声明一个常。例:

    const int len=3;

    string[] s=new string[len];

    五、要确定数组大小,可以用length来访问。例:

    int arraylength=integers.Length;

    Array.Sort(string)数组按正序排列

    Array.Reverse逆序排列

    六、多维数组

    c#支持两数组。第一种是矩形数组。例:

    string[,] strings;

    第二种多维数组是正交数组

    int[][] a=new int[3][];

    a[0]=new int[4];

    a[1]=new int[2];

    a[2]=new int[1];

    要声明一个正交三维数组应使用:

    int[][][] array;

    七、自定义类型数组的声明

    public class userinfo
    {
      string[] a=new string[3];
      string[][] b=new string[2][2];
      public void printA()
      {
        System.Console.WriteLine("this is array");
      }
    } 
    
    

    自定义数组声明:userinfo[] user=new userinfo[3];

  • 相关阅读:
    237. Delete Node in a Linked List
    430. Flatten a Multilevel Doubly Linked List
    707. Design Linked List
    83. Remove Duplicates from Sorted List
    160. Intersection of Two Linked Lists
    426. Convert Binary Search Tree to Sorted Doubly Linked List
    142. Linked List Cycle II
    类之间的关系
    初始化块
    明确类和对象
  • 原文地址:https://www.cnblogs.com/lhard/p/2137717.html
Copyright © 2011-2022 走看看