C#中獲取行列的維數和長度


 


下面是C#多維數組的一些常用屬性方法:
array.Length //獲取所有元的總數
array.GetLength(int dimension);//獲取第dimension + 1維度的元素個數,如果是二維數組,則0獲取行數,1獲取列數。
array.Rank //獲取數組的維數,二維數組則為2,三維數組則為3
array.GetUpperBound(int dimension);//獲取第dimension + 1維度的最大索引
array.GetLowund(int diamension);//獲取第dimension + 1維度的第一個元素
————————————————

    public partial class Form1 : Form
    {
        //定義一個5行3列的二維數組
        int[,] array = new int[,] 
        {
              { 1, 2, 3 },
              { 4, 5, 6 },
              { 7, 8, 9 },
              { 78, 88, 98 },
              { 78, 88, 98 }
        };
        public Form1()
        {
            InitializeComponent();

            int a = array.GetLength(0);//返回數組的第一維的長度,二維數組時返回行數,此例返回5
            int b = array.GetLength(1);//返回數組的第二維的長度,二維數組時返回列數,此例返回3
            int c = array.GetUpperBound(0);//返回第一維數組的索引上限,此例返回4
            int d = array.GetUpperBound(1); //返回第二維數組的索引上限,此例返回2

            int dimension = array.Rank;//獲取維數,此例返回2
            int colNum = array.GetUpperBound(0) + 1;//獲取指定維度的索引上限,在加上一個1就是總數,這里表示二維數組的行數,此例返回5
            int rowNum = array.GetUpperBound(1) + 1;//獲取指定維度的索引上限,在加上一個1就是總數,這里表示二維數組的列數此例返回3
            int totalNum = array.Length;//獲取整個二維數組的長度,即所有元的個數,此例返回15
        }
    }

【實例 1】在 Main 方法中定義一個存放學生成績的二維數組,並將該數組中每個學生的成績輸出。

根據題目要求,定義二維數組為 double 類型的,代碼如下。

class Program
{
    static void Main(string[] args)
    {
        double[,] points = { { 90, 80 }, { 100, 89 }, { 88.5, 86 } };
        for(int i = 0; i < points.GetLength(0); i++)
        {
            Console.WriteLine("" + (i + 1) + "個學生成績:");
            for(int j = 0; j < points.GetLength(1); j++)
            {
                Console.Write(points[i, j] + " ");
            }
            Console.WriteLine();
        }
    }
}

 

執行上面的代碼,效果如下圖所示。
image.png
在遍歷多維數組元素時使用 GetLength(維度) 方法能獲取多維數組中每一維的元素,維度也是從 0 開始的,因此在該實例中獲取數組中第一維的值時使用的是 points.GetLength(0)。

在 C# 語言中不僅支持上面給出的多維數組,也支持鋸齒型數組,即在多維數組中的每一維中所存放值的個數不同。

鋸齒型數組也被稱為數組中的數組。定義鋸齒型數組的語法形式如下。
數據類型[][] 數組名 = new 數據類型[數組長度][];
數組名[0] = new 數據類型[數組長度];

在這里,數據類型指的是整個數組中元素的類型,在定義鋸齒型數組時必須要指定維度。

【實例 2】在 Main 方法中創建一個鋸齒型數組,第一維數組的長度是 2、第二維數組的長度是 3、第三維數組的長度是 4,並直接向數組中賦值,最后輸出數組中的元素。

根據題目要求,代碼如下。

class Program
{
    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();
        }
    }
}

 

執行上面的代碼,效果如下圖所示。
image.png
鋸齒型數組中的值也可以通過循環語句來賦值,與輸岀語句類似。

在上面的實例中, arrays 數組中的元素從控制台輸入的具體語句如下。

int[][] arrays = new int[3][];
arrays[0] = new int[2];
arrays[1] = new int[3];
arrays[2] = new int[4];
for(int i = 0; i < arrays.Length; i++)
{
    Console.WriteLine("輸入數組中第" + (i + 1) + "行的元素:");
    for(int j=0;j<arrays[i].Length; j++)
    {
        arrays[i][j] = int.Parse(Console.ReadLine());
    }
    Console.WriteLine();
}

 

注:此文部分內容來源於網絡,如有不妥,請聯系刪除!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM