(1) 一維陣列的情形
/* 中括號 [] 一定緊鄰著型別 */(2) 二維陣列宣告方式:
int [] FOO = new int[5] {1, 2, 3, 4, 5};
int [] BAR = new int[] {1, 2, 3, 4, 5};
int [] foo = {1, 2, 3, 4, 5}; /* 精簡寫法 */
string [] Foo = new string[3] {"Jack", "Peter", "Tom"};
string [] Bar = {"Jack", "Peter", "Tom"}; /* 背起來 */
int [,] foo = new int[2,2] {{1,2}, {3,4}};(3) 不規則陣列(Jagged Array)宣告方式:
int [,] bar = new int[,] {{1,2}, {3,4}};
int [,] bar = {{1,2}, {3,4}};
int [][] foo = new int[3][];
/* 宣告後再逐一進行初始化 */
foo[0] = new int[2] {1,2};
foo[1] = new int[ ] {1,2,3,4};
foo[2] = {1,2,3}; /* ERROR!! 要使用 new 配置空間 */
沒有留言:
張貼留言