好吧,如果你想要一个简单的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我不想在一步之内 –