C#數組維數及不同維數中元素個數的獲取


簡單理解有關數組維數的概念:

1、編程中用到的多維的數組,最多也就是二維數組了

2、數組的維數從0開始計算

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Collections;
 4 using System.IO;
 5 using System.Security.Cryptography;
 6 using System.Text;
 7 
 8 namespace myMethod
 9 {
10     class lgs
11     {
12         static void Main()
13         {
14             int[] array0 = new int[] { 1, 2, 3, 5 };
15             int[,] array1 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };  //注意:嵌套數組的維數需要一致
16 
17             //打印數組的維數
18             Console.WriteLine(array0.Rank);     // 1
19             Console.WriteLine(array1.Rank);     // 2
20 
21             //打印不同維中的元素個數,這里需要注意下數組的維數應該如何理解
22             Console.WriteLine(array0.GetLength(0));     //第一維的元素個數  4
23             Console.WriteLine(array1.GetLength(0));     //第一維的元素個數  2
24             Console.WriteLine(array1.GetLength(1));     //第二維的元素個數  3
25 
26             Console.ReadKey();
27         }
28     }
29 }

 


免責聲明!

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



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