如何在 C# 中求数组的秩
阵列。等级属性用于获取阵的等级。秩是数组的维数。例如,一维数组返回 1,二维数组返回 2,依此类推。
语法:
public int Rank { get; }
属性值:返回系统类型数组的秩(维数)。Int32 。
以下程序说明了上述属性的使用:
例 1:
// C# program to illustrate the
// Array.Rank Property
using System;
namespace geeksforgeeks {
class GFG {
// Main Method
public static void Main()
{
// declares a 1D Array of string.
string[] weekDays;
// allocating memory for days.
weekDays = new string[] {"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat" };
// using Rank Property
Console.WriteLine("Dimension of weekDays array: "
+ weekDays.Rank);
}
}
}
Output:
Dimension of weekDays array: 1
例 2:
// C# program to illustrate the
// Array.Rank Property
using System;
namespace geeksforgeeks {
class GFG {
// Main Method
public static void Main()
{
// declaring an 2-D array
int[, ] arr2d = new int[4, 2];
// declaring an 3-D array
int[,, ] arr3d = new int[4, 2, 3];
// declaring an jagged array
int[][] jdarr = new int[2][];
// using Rank Property
Console.WriteLine("Dimension of arr2d array: "
+ arr2d.Rank);
Console.WriteLine("Dimension of arr3d array: "
+ arr3d.Rank);
// for the jagged array it
// will always return 1
Console.WriteLine("Dimension of jdarr array: "
+ jdarr.Rank);
}
}
}
Output:
Dimension of arr2d array: 2
Dimension of arr3d array: 3
Dimension of jdarr array: 1
注:
- 一个交错数组(数组的数组)是一维数组,所以它的 Rank 属性的值是 1。
- 检索该属性的值是一个 O(1)操作。
参考:
版权属于:月萌API www.moonapi.com,转载请注明出处