zoukankan      html  css  js  c++  java
  • C#规则数组和不规则数组

    见下列代码:

            static void Main(string[] args)
            {
                int[,] a = new int[,] { { 0, 1, 2, 3 }, { 2, 3, 4, 5 }, { 3, 4, 5, 6 } };
                Console.WriteLine("规则数组:");
                Console.WriteLine(a.Length);
                Console.WriteLine(a.GetLength(0));
                Console.WriteLine(a.GetLength(1));
    
                int[][] b = new int[3][];
                b[0] = new int[4];
                b[1] = new int[5];
                b[2] = new int[4];
                //b[3] = new int[5];
                Console.WriteLine("不规则数组:");
                Console.WriteLine(b.Length);
                Console.WriteLine(b.GetLength(0));
                //调用将会出错: Console.WriteLine(b.GetLength(1));
                Console.WriteLine(b[0].Length);
                Console.Read();
            }

    输出结果为:
    规则数组:

    12

    3

    4

    不规则数组

    3

    3

    4

    第一个规则数组很好理解,但是第二个不规则数组,为什么b.GetLength(0)和b[0].Length不一样。

    调试监视变量结果如下图:

    通过上图可以发现b.GetLength(0)其实取的是b数组里面共有几个一维数组,这里我们定义 int[][] b = new int[3][];所以结果为3

    而b[0].Length则是取的b数组里面的第一个一维数组的长度,这里我们定义了  b[0] = new int[4];所以结果为4

    在这里我们可以给b里面的第一个一维数组赋值:b[0][0] = 5;或者   b[0] = new int[4]{1,2,3,4};

    这样b[0]数组里面就有值了。

  • 相关阅读:
    FZU2150 Fire Game
    POJ3414 Pots
    POJ3087 Shuffle'm Up
    POJ3126 Prime Path
    POJ1426 Find The Multiple
    POJ3279 Fliptile
    甘特图实用技巧——项目进度一目了然!
    连设计图都不会画,你还想做“系统架构师”?
    java中list和map的底层实现原理
    redis四种部署方式
  • 原文地址:https://www.cnblogs.com/bianlan/p/2816590.html
Copyright © 2011-2022 走看看