如何在 C# 中查找锯齿阵列的长度和秩?


首先,设置一个锯齿阵列。

int[][] arr = new int[][] {
   new int[] {
      0,
      0
   }, new int[] {
      1,
      2
   },
   new int[] {
      2,
      4
   }, new int[] {
      3,
      6
   }, new int[] {
      4,
      8
   }
};

现在,使用 GetLowerBound()、GetUpperBound() 和 Rank 属性获取阵列的长度和秩,如下面的代码所示 −

示例

 实时演示

using System;
public class Program {
   public static void Main() {
      int[][] arr = new int[][] {
         new int[] {
            0,
            0
         }, new int[] {
            1,
            2
         },
         new int[] {
            2,
            4
         }, new int[] {
            3,
            6
         }, new int[] {
            4,
            8
         }
      };
      // Length
      Console.WriteLine("Length:" + arr.Length);
      Console.WriteLine("Upper Bound: {0}", arr.GetUpperBound(0).ToString());
      Console.WriteLine("Lower Bound: {0}", arr.GetLowerBound(0).ToString());
      Console.WriteLine("Dimensions of Array : " + arr.Rank);
   }
}

输出

Length:5
Upper Bound: 4
Lower Bound: 0
Dimensions of Array : 1

更新日期:22-6-2020

295 次浏览

开启您的 职业生涯

完成课程以获得认证

开始
广告
© . All rights reserved.