如何查詢 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

更新於: 2020-06-22

295 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始吧
廣告