其實在大多數工作中我們能通過前處理來確定我們的數組有多大,這樣我們就可以聲明相應大小的數組了。我感覺這種“動態”數組就夠我用了。比如我要處理excel中數據,數據有m行*n列,這樣我就可以通過讀取excel來確定m和n的大小,然后再聲明m行n列的二維數組,這樣就可以處理了啊。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1array 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //Random r = new Random(); 14 Console.WriteLine("請輸入二維數組的行數:"); 15 int i = int.Parse(Console.ReadLine()); //3; //r.Next() % 10 + 10; 16 Console.WriteLine("請輸入二維數組的列數:"); 17 int m = int.Parse(Console.ReadLine ());//4; 18 int[,] array1 = new int[i,m]; 19 for (int fi = 0; fi < i;fi++ ) 20 { 21 for (int fj = 0; fj < m; fj++) 22 { 23 array1[fi, fj] = fi + fj; 24 Console.Write("{0}\t ",array1[fi, fj]); 25 } 26 Console.WriteLine(); 27 28 } 29 //foreach (int arr in array1) 30 //{ 31 32 // Console.WriteLine(arr); 33 // Console.WriteLine(); 34 //} 35 //for (int j = 0; j < i; j++) 36 //{ 37 // array1[j] = j;//r.Next(0,10); 38 // Console.WriteLine(array1[j]); 39 // Console.WriteLine(); 40 //} 41 Console.ReadKey(); 42 } 43 } 44 }