摘要:(200-07-02) C#.NET 陣列
一般陣列宣告
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace csmod03 { class Program { static void Main(string[] args) { //陣列 Int32[] iar = null; //建構元素數量 iar = new Int32[5]; //5個元素 iar[0] = 100; iar[1] = 200; //順序迴圈 Int32 r; for ( r= 0; r < iar.Length;r++) { //輸出 System.Console.WriteLine(iar[r].ToString()); } System.Console.WriteLine(r); } } }
多維度陣列應用
using System.Collections.Generic; using System.Linq; using System.Text; //多維度陣列應用 namespace csmod03 { class TestMultiDimension { //主程式 public static void Main() { //定義多維度 Int32[,] iar=new Int32[3,3]; System.Console.WriteLine(iar.Length); //總元素數量 //assign 內容 iar[0, 0] = 100; iar[0, 1] = 200; iar[0, 2] = 300; iar[1, 0] = 400; iar[1, 1] = 500; iar[1, 2] = 600; iar[2, 0] = 700; iar[2, 1] = 800; iar[2, 2] = 900; //逐一參考(次數迴圈) System.Console.WriteLine(); System.Console.WriteLine(iar.GetLength(0)); for (Int32 p = 0; p <iar.Rank; p++) { for (Int32 r = 0; r < iar.GetLength(p); r++) //取出某一層的原宿數量GetLength(n) { System.Console.WriteLine(iar[p, r].ToString()); } } } } }
不規則維度應用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace csmod03 { //不規則維度 class TestJagg { public static void Main() { Int32[][] iar = null; iar = new Int32[3][]; iar[0] = new Int32[3]; iar[1] = new Int32[4]; iar[2] = new Int32[5]; System.Console.WriteLine(iar.Length); //assign iar[0][0] = 100; iar[0][1] = 200; iar[0][2] = 300; iar[1][0] = 400; iar[1][1] = 500; iar[1][2] = 600; iar[1][3] = 700; for (Int32 p = 0; p < iar.Length; p++) { for (Int32 r = 0; r < iar[p].Length; r++) { System.Console.WriteLine(iar[p][r].ToString()); } } } } }