[2 [2
c#'s concat
x = new int [] {1,2,3}; int [] y = new int [] {4,5}; int [] z = new int [x.length y.length]; array.copy(x,z,x.length); array.copy(y,0,z,x.length,y.length); debug.assert(z.SequenceEqual(new Int [] {1,2,2,3,4,5}));
此方法直接分配了一个足够大小的新数组,以保持[ y
。 It then leverages
int[] x = new int[] { 1, 2, 3 };
int[] y = new int[] { 4, 5 };
int[] z = new int[x.Length y.Length];
Array.Copy(x, z, x.Length);
Array.Copy(y, 0, z, x.Length, y.Length);
Debug.Assert(z.SequenceEqual(new int[] { 1, 2, 3, 4, 5 }));
y into z
.这避免了创建中间数组的创建,从而可以更快地执行,尤其是使用较大的数据集。
重要考虑:
concat 仍然适用于较小的数组,而性能并不关键,上面的方法为涉及大型数组的方案提供了实质性的性能优势。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3