• C# 基础知识 数组


    类 的 静态构造函数 和 普通构造函数。在类实例化时,会先执行静态构造函数;类没有实例化时,只会调用静态构造函数;

        public class A
        {
            public static int i = 0;   public int j = 0;
            public A() {   j = i + 1;   }     static A()   { i = i + 1;   }
        }
        A a = new A(); 
        int i = a.j; //i = 2 ;
        int i1 = A.i; // i1 = 1 ;

    ----------------------------------------------------------------------------------------------------------------------------------------------

    //定义多维数组
    数据类型[ , , ...] 数组名;
    //创建多维数组并初始化
    数据类型[ , , ...] 数组名 = new 数据类型[m,n,...] {{ , , ...},{ , , ...}}; 

    二维数组

    3 行 2列 视图;

    创建5行2列的二维数组

      static void Main(string[] args)
            {
                //创建5行2列的二维数组
                int[,] a = new int[5, 2] { { 0, 0 }, { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 } };
    
                int len1 = a.GetLength(0);//获取  行数量:5行
                int len2 = a.GetLength(1);//获取  列数量:2列
                for (int i = 0; i < len1; i++)
                {
                    Console.WriteLine("" + (i + 1) + "行:");
                    for (int j = 0; j < len2; j++)
                    {
                        Console.Write(a[i, j] + " ");
                    }
                    Console.WriteLine();
                }
                Console.ReadKey();
            }

    ◆ 三维数组:2行2列3组    

        static void Main(string[] args)
        {
               int[, ,] a = new int[2, 2,3]{ {1,2,3 4,5,6 } } , {7,8,9 2,3,4 } } };
               int len1 = a.GetLength(0);//获取 行数量:2行
               int len2 = a.GetLength(1);//获取 列数量:2列
               int len3 = a.GetLength(2);//获取 组数量:3组
               for (int i = 0 ;  i < len1 ; i++ )
               {
                     Console.WriteLine("第"+i+"行");
                     for (int t = 0; t < len2; t++ )
                     {
                          Console.Write("第"+t+"列");
                          for (int v = 0 ;  v < len3 ; v++ )
                          {
                                Console.Write(ai , t , v ]+",");
                          }
                         Console.Write(" ");
                     }
                     Console.WriteLine();
               }
              Console.ReadKey();
        }

    ◆ 锯齿型数组

    数据类型[][]  数组名 = new 数据类型[数组长度][];
    数组名[0] = new 数据类型[数组长度];

    static void Main(string[] args)
            {
                int[][] arrays = new int[3][];
                arrays[0] = new int[] { 1, 2 };
                arrays[1] = new int[] { 3, 4, 5 };
                arrays[2] = new int[] { 6, 7, 8, 9 };
                for (int i = 0; i < arrays.Length; i++)
                {
                    Console.WriteLine("输出数组中第" + (i + 1) + "行的元素:");
                    for (int j = 0; j < arrays[i].Length; j++)
                    {
                        Console.Write(arrays[i][j] + " ");
                    }
                    Console.WriteLine();
                }
            }

    ----------------------------------------------------------------------------------------------------------------------------------------------

  • 相关阅读:
    我的架构经验系列文章 前端架构
    我的架构经验系列文章 后端架构 安全层面
    Adhesive框架系列文章报警服务模块使用和实现
    Adhesive框架系列文章Mongodb数据服务模块实现(上)
    在Silverlight程序中使用Thread一个很容易被忽略的问题
    .net(偏web) vs j2ee的一些框架选型
    Wcf扩展
    Adhesive框架系列文章内存队列服务模块使用和实现
    【翻译】C#编程语言和JAVA编程语言的比较(下)
    在测试Adhesive的时候发现一个Mongodb官方驱动1.1.0.4184比较严重的BUG
  • 原文地址:https://www.cnblogs.com/lanyubaicl/p/11275162.html
Copyright © 2020-2023  润新知