如何從 C# 中的矩形陣列中訪問元素?
要從矩形陣列中訪問元素,你只需要設定想要獲取元素的索引。多維陣列也稱為矩形陣列 −
a[0,1]; // second element
下面是一個示例,展示瞭如何在 C# 中使用矩形陣列並訪問元素 −
示例
using System; namespace Demo { class Program { static void Main(string[] args) { int[,] a = new int[3, 3]; a[0,0]= 10; a[0,1]= 20; a[0,2]= 30; a[1,0]= 40; a[1,1]= 50; a[1,2]= 60; a[2,0]= 70; a[2,1]= 80; // accessing element a[2,2]= 90; int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]); } } int ele = a[0,1]; Console.WriteLine("Second element: "+ele); Console.ReadKey(); } } }
廣告