好吧,如果你想要一個簡單的1D陣列,嘗試SelectMany
:
int[] a = { 1, 2, 3 };
int[] b = { 4, 5, 6 };
int[] c = { 7, 8, 9 };
// d == {1, 2, 3, 4, 5, 6, 7, 8, 9}
int[] d = new[] { a, b, c } // initial jagged array
.SelectMany(item => item) // flattened
.ToArray(); // materialized as a array
如果你想有一個鋸齒狀陣列(陣列陣列)的
// d == {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
// notice the declaration - int[][] - array of arrays of int
int[][] d = new[] { a, b, c };
如果你想追加數組有條件地,不是一蹴而就的,array不是d
的集合類型; List<int>
List<int[]>
或將有助於更好:
// 1d array emulation
List<int> d = new List<int>();
...
d.AddRange(a);
d.AddRange(b);
d.AddRange(c);
或者
// jagged array emulation
List<int[]> d = new List<int[]>();
...
d.Add(a); //d.Add(a.ToArray()); if you want a copy of a
d.Add(b);
d.Add(c);
'd = {{1,2,3},{4,5,6},{7,8,9}}; ''不是'int []' –
應該是一個*鋸齒狀數組,即數組{{1,2,3},{4,5,6},{7,8,9}} '或者只是一個簡單的** 1D **:'{1,2,3,4,5,6,7,8,9}'? –
@Roma我不想在一步之內 –